uber zap logger使用指南

本文介绍了Uber的zap日志库在Golang中的使用,重点对比了NewDevelopment和NewProduction两种配置的区别,前者包含文件信息,后者则不包含。此外,还详细讲解了如何动态改变日志的打印级别,通过创建并调整zap的日志级别原子变量atom,实现日志级别的动态切换。代码示例可在zaplog.go的zaplogRoteChangeLevel函数中查看。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

zap logger项目地址 go.uber.org/zap

NewDevelopment和NewProduction区别

zap.NewDevelopment() 包含代码中文件信息

2018-01-18T15:40:05.991+0800    INFO    tool/zaplog.go:83       to sugar failed to fetch URLurlhttp://example.comattempt3backoff1s
2018-01-18T15:40:05.991+0800    INFO    tool/zaplog.go:88       to sugar failed to fetch URL    {"url": "http://example.com", "attempt": 3, "backoff": "1s"}
2018-01-18T15:40:05.991+0800    INFO    tool/zaplog.go:94       to desugar failed to fetch URL  {"url": "http://example.com", "attempt": 3, "backoff": "1s"}

zap.NewProduction() 去除了文件信息

{"level":"info","ts":1516261205.991458,"caller":"tool/zaplog.go:109","msg":"to sugar failed to fetch URLurlhttp://example.comattempt3backoff1s"}
{"level":"info","ts":1516261205.9914737,"caller":"tool/zaplog.go:114","msg":"to sugar failed to fetch URL","url":"http://example.com","attempt":3,"backoff":1}
{"level":"info","ts":1516261205.991483,"caller":"tool/zaplog.go:120","msg":"to desugar failed to fetch URL","url":"http://example.com","attempt":3,"backoff":1}

动态改变日志的打印级别

利用 zap中的atom,
首先创建一个atom

atom := zap.NewAtomicLevel()

创建core的时候,第三个参数传递atom而不是zap.DebugLevel这类的固定值。

    core := zapcore.NewCore(
        zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig()), //开发者Encoder,包含函数调用信息
        // zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
        w,
        // zap.InfoLevel, //info,error
        atom, //debug,info,warn,error
    )
    logger := zap.New(core)

想要改变日志的级别

atom.SetLevel(zap.DebugLevel)

完整代码参见zaplog.go中的zaplogRoteChangeLevel
由下面的输出可以看出的确改变了日志级别。

{"L":"INFO","T":"2018-01-18T16:18:05.324+0800","M":"to desugar failed to fetch URL","url":"http://example.com","attempt":3,"backoff":"1s"}

{"L":"WARN","T":"2018-01-18T16:18:05.324+0800","M":"to desugar failed to fetch URL","url":"http://example.com","attempt":3,"backoff":"1s"}
{"L":"ERROR","T":"2018-01-18T16:18:05.324+0800","M":"to desugar failed to fetch URL","url":"http://example.com","attempt":3,"backoff":"1s"}
{"L":"DEBUG","T":"2018-01-18T16:18:05.324+0800","M":"to desugar failed to fetch URL","url":"http://example.com","attempt":3,"backoff":"1s"}

{"L":"INFO","T":"2018-01-18T16:18:05.324+0800","M":"to desugar failed to fetch URL","url":"http://example.com","attempt":3,"backoff":"1s"}
{"L":"WARN","T":"2018-01-18T16:18:05.324+0800","M":"to desugar failed to fetch URL","url":"http://example.com","attempt":3,"backoff":"1s"}
{"L":"ERROR","T":"2018-01-18T16:18:05.324+0800","M":"to desugar failed to fetch URL","url":"http://example.com","attempt":3,"backoff":"1s"}
{"L":"ERROR","T":"2018-01-18T16:18:05.324+0800","M":"to desugar failed to fetch URL","url":"http://example.com","attempt":3,"backoff":"1s"}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值