[uber-zap/part1]简单的使用

47次阅读

共计 4090 个字符,预计需要花费 11 分钟才能阅读完成。

说明
之前翻译的一个教程 (没有备份原地址,梯子被封了)。原地址找到后补上
zap 的安装

开源仓库地址 https://github.com/uber-go/zap

安装 go get -u go.uber.org/zap

默认的三种设置

Example
Development
Production

Example
func main() {
log := zap.NewExample()
log.Debug(“This is a DEBUG message”)
log.Info(“This is an INFO message”)
log.Info(“This is an INFO message with fields”, zap.Strings(“region”, []string{“us_west”}), zap.Int(“id”, 2))
log.Warn(“This is a WARN message”)
log.Error(“This is an ERROR message”)
log.DPanic(“This is a DPANIC message”)
}
输出:
{“level”:”debug”,”msg”:”This is a DEBUG message”}
{“level”:”info”,”msg”:”This is an INFO message”}
{“level”:”info”,”msg”:”This is an INFO message with fields”,”region”:[“us_west”],”id”:2}
{“level”:”warn”,”msg”:”This is a WARN message”}
{“level”:”error”,”msg”:”This is an ERROR message”}
{“level”:”dpanic”,”msg”:”This is a DPANIC message”}

Development
func main() {
log, _ := zap.NewDevelopment()
log.Debug(“This is a DEBUG message”)
log.Info(“This is an INFO message”)
log.Info(“This is an INFO message with fields”, zap.Strings(“region”, []string{“us_west”}), zap.Int(“id”, 2))
log.Warn(“This is a WARN message”)
log.Error(“This is an ERROR message”)
//log.Fatal(“This is a FATAL message”) // 程序在这停止如果不注释
//log.DPanic(“This is a DPANIC message”) // 程序在这停止如果不注释
//log.Panic(“This is a PANIC message”) // 程序在这停止如果不注释
}
输出
2018-10-30T17:14:22.459+0800 DEBUG development/main.go:7 This is a DEBUG message
2018-10-30T17:14:22.459+0800 INFO development/main.go:8 This is an INFO message
2018-10-30T17:14:22.459+0800 INFO development/main.go:9 This is an INFO message with fields {“region”: [“us_west”], “id”: 2}
2018-10-30T17:14:22.459+0800 WARN development/main.go:10 This is a WARN message
main.main
/home/wsl/gopath/src/wsl_learn/zap-example/development/main.go:10
runtime.main
/usr/local/go/src/runtime/proc.go:201
2018-10-30T17:14:22.459+0800 ERROR development/main.go:11 This is an ERROR message
main.main
/home/wsl/gopath/src/wsl_learn/zap-example/development/main.go:11
runtime.main
/usr/local/go/src/runtime/proc.go:201
Production
func main() {
log, _ := zap.NewProduction()
log.Debug(“This is a DEBUG message”)
log.Info(“This is an INFO message”)
log.Info(“This is an INFO message with fields”, zap.Strings(“region”, []string{“us-west”}), zap.Int(“id”, 2))
log.Warn(“This is a WARN message”)
log.Error(“This is an ERROR message”)
//log.Fatal(“This is a FATAL message”) // 程序在这停止如果不注释
log.DPanic(“This is a DPANIC message”)
//log.Panic(“This is a PANIC message”) // 程序在这停止如果不注释
}
输出
{“level”:”info”,”ts”:1540891173.3190675,”caller”:”production/main.go:8″,”msg”:”This is an INFO message”}
{“level”:”info”,”ts”:1540891173.3191047,”caller”:”production/main.go:9″,”msg”:”This is an INFO message with fields”,”region”:[“us-west”],”id”:2}
{“level”:”warn”,”ts”:1540891173.3191204,”caller”:”production/main.go:10″,”msg”:”This is a WARN message”}
{“level”:”error”,”ts”:1540891173.31913,”caller”:”production/main.go:11″,”msg”:”This is an ERROR message”,”stacktrace”:”main.main\n\t/home/wsl/gopath/src/wsl_learn/zap-example/production/main.go:11\nruntime.main\n\t/usr/local/go/src/runtime/proc.go:201″}
{“level”:”dpanic”,”ts”:1540891173.3191597,”caller”:”production/main.go:13″,”msg”:”This is a DPANIC message”,”stacktrace”:”main.main\n\t/home/wsl/gopath/src/wsl_learn/zap-example/production/main.go:13\nruntime.main\n\t/usr/local/go/src/runtime/proc.go:201″}
三种预设方式的对比

Example 和 Production 使用的是 json 格式输出,Development 使用行的形式输出

Development

从警告级别向上打印到堆栈中来跟踪
始终打印包 / 文件 / 行(方法)
在行尾添加任何额外字段作为 json 字符串
以大写形式打印级别名称
以毫秒为单位打印 ISO8601 格式的时间戳

Production

调试级别消息不记录
Error,Dpanic 级别的记录,会在堆栈中跟踪文件,Warn 不会
始终将调用者添加到文件中
以时间戳格式打印日期
以小写形式打印级别名称

Sugar Logger
默认的 zap 记录器需要结构化标签,即对每个标签,需要使用特定值类型的函数
logger.Info(“This is an INFO message with fields”,
zap.String(“region”, “us-west”),
zap.Int(“id”, 2))
虽然会显的很长,但是对性能要求较高的话,这是最快的选择。也可以使用 suger logger, 它基于 printf 分割的反射类型检测,提供更简单的语法来添加混合类型的标签
func main() {
logger, _ := zap.NewDevelopment()
slogger := logger.Sugar()

slogger.Info(“Info() uses sprint”)
slogger.Infof(“Infof() uses %s”, “sprintf”)
slogger.Infow(“Infow() allows tags”, “name”, “Legolas”, “type”, 1)
}
输出
func main() {
logger, _ := zap.NewDevelopment()
slogger := logger.Sugar()

slogger.Info(“Info() uses sprint”)
slogger.Infof(“Infof() uses %s”, “sprintf”)
slogger.Infow(“Infow() allows tags”, “name”, “Legolas”, “type”, 1)
}
如果需要,可以随时使用记录器上的.Desugar() 方法从 sugar logger 切换到标准记录器
log := slogger.Desugar()

log.Info(“After Desugar; INFO message”)
log.Warn(“After Desugar; WARN message”)
log.Error(“After Desugar; ERROR message”)

正文完
 0