介绍
zap.Logger包如何在golang中使用?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
杀死。记录器是去语言中相对日志库中性能最高的。那么如何开始使用?
不多说直接上代码:
import ( “编码/json"才能 “才能fmt" “才能log" “;go.uber.org/zap"才能; “;go.uber.org/zap/zapcore"才能; ) var Logger * zap.Logger func InitLogger (), {//才能,日志地址,“out.log",自定义 lp 才能:=Conf.Common.LogPath//才能,日志级别,调试错误,信息 lv 才能:=Conf.Common.LogLevel//才能,是否,调试 isDebug 才能:=,真的 if 才能;Conf.Common.IsDebug  !=, true { ,,isDebug =false ,,} initLogger才能(lp, lv,, isDebug) log.SetFlags才能(log.Lmicroseconds |, log.Lshortfile |, log.LstdFlags) } func initLogger(字符串,lp lv 字符串,isDebug bool), { var 才能;js 字符串 if 才能;isDebug  { ,,js =, fmt.Sprintf (“{ ,才能“level":,“% s" ,才能“encoding":,“json" ,才能“outputPaths":, (“stdout"), ,才能“errorOutputPaths":, (“stdout") ,,},lv) ,,},{else ,,js =, fmt.Sprintf (“{ ,才能“level":,“% s" ,才能“encoding":,“json" ,才能“outputPaths":, (“% s"), ,才能“errorOutputPaths":, (“% s") ,,}’,lv, lp, lp) ,,} var 才能;cfg  zap.Config if 才能;err :=, json.Unmarshal([]字节(js),,, cfg);, err !=, nil { ,,恐慌(err) ,,} 时间=cfg.EncoderConfig 才能;zap.NewProductionEncoderConfig () cfg.EncoderConfig.EncodeTime 才能=zapcore.ISO8601TimeEncoder var 才能;err 错误 记录器,才能,err =, cfg.Build () if 才能;err  !=, nil { ,,log.Fatal (“init  logger 错误:,,,,呃) ,,} }
如何使用:
func TestInitLogger (t * testing.T), { InitLogger才能(“out.log",,“DEBUG",,假) s 才能;:=,[]字符串{ ,才能“hello info" ,才能“hello error" ,才能“hello debug" ,才能“hello fatal" ,,} Log.Info才能(“信息:“,,zap.String (“s",, s [0])) Log.Error才能(“信息:“,,zap.String (“s",, s [1])) Log.Debug才能(“信息:“,,zap.String (“s",, s [2])) Log.Fatal才能(“信息:“,,zap.String (“s",, s [3])) }
输出:
{“level":“info",“ts":“2017 - 10 - 25, 13:45:42.332",“caller":“记录器/logger_test.go: 16“,“msg":“信息:“,“s":“hello info"} {“level":“error",“ts":“2017 - 10 - 25, 13:45:42.396",“caller":“记录器/logger_test.go: 17“,“msg":“信息:“,“s":“hello error",“stacktrace":“;go.uber.org/zap.Stack道明:\ n \/gopath/src/go.uber.org/zap/field.go:191 \ngo.uber.org/zap。(*记录器)支票道明:\ n \/gopath/src/go.uber.org/zap/logger.go:301 \ngo.uber.org/zap。(*记录器). error道明:\ n \/gopath/src/go.uber.org/zap/logger.go:202 \ngithub.com/corego/hermes/logger.TestInitLogger道明:\ n \/gopath/src/github.com/corego/hermes/logger/logger_test.go:17 \ ntesting.tRunner \ n \ tD:/Program Files (x86)/去/src/测试/testing.go: 746年“} {“level":“debug",“ts":“2017 - 10 - 25, 13:45:42.396",“caller":“记录器/logger_test.go: 18“,“msg":“信息:“,“s":“hello debug"} null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullzap.Logger包如何在golang中使用