Golang Log 学习笔记

官方Doc链接

http://godoc.golangtc.com/pkg/log/

简介

Package log implements a simple logging package. It defines a type, Logger, with methods for formatting output. It also has a predefined ‘standard’ Logger accessible through helper functions Print[f|ln], Fatal[f|ln], and Panic[f|ln], which are easier to use than creating a Logger manually. That logger writes to standard error and prints the date and time of each logged message. The Fatal functions call os.Exit(1) after writing the log message. The Panic functions call panic after writing the log message.

译 :
log 包实现了一个简单的日志库。 它定义了提供格式化输出接口的类型 : Logger。它还提供了一个预先定义好的“标准”Logger,这个Logger往标准出错打印每一条日志以及他的时间。 可以使用接口 : Print[f|ln], Fatal[f|ln], 和 Panic[f|ln] 来直接使用”标准”Logger, 比起自己手动创建Logger, 他们要方便的多。 Fatal 接口在写完日志后会调用os.Exit(1)Panic 接口在写完日志后会调用panic。( 如果没有配置panic, 也会导致程序退出)

[f|ln]

对于本包的接口,往往会有3个版本 : XXX , XXXf 和 XXXln , 含义分别是 :打印 , 格式化打印, 打印并换行 。

接口

打印不同等级的log
  • Print[f|ln] 打印
  • Panic[f|ln] 恐慌
  • Fatal[f|ln] 致命
  • Output 类似Println , 可以指定打印调用栈的特定深度的信息
设置logger熟悉
  • SetFlags 陪自己你需要的打印熟悉
    • Ldate = 1 << iota // the date in the local time zone: 2009/01/23
    • Ltime // the time in the local time zone: 01:23:23
    • Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime.
    • Llongfile // full file name and line number: /a/b/c/d.go:23
      *Lshortfile // final file name element and line number: d.go:23. overrides Llongfile
    • LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone
    • LstdFlags = Ldate | Ltime // initial values for the standard logger
  • SetOutput 日志输出到何方, 默认stderr, 任何实现 io.Writer 接口的对象
  • SetPrefix 配置你的专属前缀

定制自己的Logger

package log_test

import "log"
import "os"
import "runtime"
import "fmt"

type LogServer struct {
    logger     *log.Logger
    file       *os.File
    thread_num uint32
    file_name  chan string
    log_buffer chan string
    End        chan bool
}

func NewLogger() (*log.Logger, *LogServer) {
    new_log := new(LogServer)
    new_log.logger = log.New(new_log, "", log.Ldate|log.Ltime|log.Lshortfile)
    new_log.file_name = make(chan string)
    new_log.log_buffer = make(chan string )
    new_log.End = make(chan bool)
    go new_log.InitLogThread()
    return new_log.logger, new_log
}

func (this *LogServer) InitLogThread() {
    for {
        select {
        case file_name , ok := <-this.file_name:
            if ok {
                this.UseFile(file_name)
            }else {
                fmt.Println("NO")
            }
        case a_log , ok := <-this.log_buffer:
            if ok {
                this.RealLog(a_log)
            }else {
                fmt.Println("NO")
            }
        case end , ok:= <-this.End:
            if end && ok {
                break
            }else {
                fmt.Println("NO")
            }
        }
    }
}

func (this *LogServer) UseFile(file_name string) {
    t_file , err := os.Create(file_name)
    if err != nil || t_file == nil {
        return
    }else {
        if this.file != nil {
            this.file.Close()
        }
        this.file = t_file
    }
}

func (this *LogServer) RealLog(p string) {
    if this.file != nil {
        this.file.Write([]byte(p))
    }else {
        fmt.Println(string(p))
    }
}

func (this *LogServer) Write(p []byte) (n int, err error) {
    this.log_buffer <- string(p)
    return len(p), nil
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值