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

小龙虾 · · 1816 次点击 · · 开始浏览    
这是一个创建于 的文章,其中的信息可能已经有所发展或是发生改变。

说明

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

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"}

三种预设方式的对比

  • ExampleProduction使用的是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")

有疑问加站长微信联系(非本文作者)

本文来自:Segmentfault

感谢作者:小龙虾

查看原文:[uber-zap/part1]简单的使用

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

1816 次点击  ∙  1 赞  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传