golang log

package log                                                                                                                                                                                                 

import (
    "log"
    "os"
    "time"
)

var (
    debug_log  = "debug.log"
    log_format = "2006-01-02-15"
)

var fileHandle *os.File
var cs chan string

func init() {
    file_name := getFileName()
    fileHandle, _ = os.OpenFile(file_name, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0755)
    log.SetOutput(fileHandle)
    cs = make(chan string, 1024)
    go WriteChan(cs)

}

func getFileName() string {
    now := time.Now()
    date := now.Format(log_format)
    file_path := "/your/local/dir/"
    filename := file_path + debug_log + "." + date

    return filename
}

func WriteDebugLog(content string) {

    //check log file
    curFileName := getFileName()

    if _, err := os.Stat(curFileName); os.IsNotExist(err) {
        file_name := getFileName()
        fileHandle, _ = os.OpenFile(file_name, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0755)
        log.SetOutput(fileHandle)
    }
    //WriteSingleLog(content)
    cs <- content
}

func WriteChan(cs chan string) {
    for {
        select {
        case con := <-cs:
            WriteSingleLog(con)
            //beego.Debug(len(cs))
        }
    }
}

func WriteSingleLog(content string) {
    log.Println(content)
}

  

时间: 2024-08-30 09:21:24

golang log的相关文章

Golang中使用log(一):Golang 标准库提供的Log

Golang的标准库提供了log的机制,但是该模块的功能较为简单(看似简单,其实他有他的设计思路).不过比手写fmt. Printxxx还是强很多的.至少在输出的位置做了线程安全的保护.其官方手册见Golang log (天朝的墙大家懂的).这里给出一个简单使用的例子: package main import ( "log" ) func main(){ log.Fatal("Come with fatal,exit with 1 \n") } 编译运行后,会看到程

golang之log rotate

操作系统: CentOS 6.9_x64 go语言版本: 1.8.3 问题描述 golang的log模块提供的有写日志功能,示例代码如下: /* golang log example E-Mail : [email protected] */ package main import ( "log" "os" ) func main() { logFile,err := os.Create("test1.log") defer logFile.Cl

go log

go自带log包 /* golang log example E-Mail : [email protected] */ package main import ( "log" "os" ) func main() { logFile,err := os.Create("test1.log") defer logFile.Close() if err != nil { log.Fatalln("open file error!"

Golang学习笔记--log包

个人站:http://www.cloudnoter.com/?p=137 一.快速使用 Golang的log包短小精悍,可以非常轻松的实现日志打印转存功能.不用多说,log支持并发操作(即协程安全-相对于JAVA中的线程安全而言),其结构定义如下: type Logger struct { mu sync.Mutex // ensures atomic writes; protects the following fields prefix string // prefix to write a

Golang中使用log(二):Golang 标准库log的实现

前一篇文章我们看到了Golang标准库中log模块的使用,那么它是如何实现的呢?下面我从log.Logger开始逐步分析其实现. 其源码可以参考官方地址 1.Logger结构 首先来看下类型Logger的定义: type Logger struct { mu sync.Mutex // ensures atomic writes; protects the following fields prefix string // prefix to write at beginning of each

Golang之beego读取配置信息,输出log模块

1,准备好配置文件 [server] listen_ip = "0.0.0.0" listen_port = 8888 [logs] log_level=debug log_path=./logs/logagent.log [collect] log_path=D:\project\logs\logagent.log topic=nginx_log chan_size=100 通过golang读取配置文件 package main import ( "fmt" &q

golang解析git log时间

最近碰到一个case,需要根据时间点查找commitid以及commit深度 ,找到一个方法,做个记录免得忘了 问题:golang无法使用time package直接parse gitlog中的date(Date:   Mon Dec 3 14:50:03 2018 +0800) 原因:gitlog中的date默认遵循的是rfc 2822格式规则,但是golang没有rfc2822的loyout,无法解析 方法: git log指令:git log时加上--date=format:'%Y-%m-

Golang官方log包详解

Golang官方log包详解 以下全是代码, 详解在注释中, 请从头到尾看 // Copyright 2009 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. // Package log implements a simple logging package. I

Golang下的Log处理

后端系统中的Log是相当重要的,做过高并发服务的同学都会认同这一点.相对而言,调试已经用处不大了,对于这样的项目,我现在也习惯了这种开发方式,有两个原因: 1.debug只能在开发环境,到产线环境就不灵了. 2.在并行计算下,debug可能无法发现潜在的问题. 有了以前做node.js的经验,放弃debug没有想象中那么可怕,只要我们程序设计合理,结构清晰,日志完整,看到异常信息后基本都可以快速定位问题所在. 做Java的时候有log4j,做.NET程序的时候有log4net, 做Node.js