英文:
How can I use go.uber.org/zap lib to print different color with different log level and append log to different file depend on the log level?
问题
我开始在我的Go项目中使用zap日志库。我想根据日志级别在tty控制台上打印不同的颜色。
我发现zap/internal/color
包可以为字符串显示不同的颜色,但我想要根据日志级别改变颜色。
我还想将日志写入一些具有不同日志级别的日志文件中。
如何初始化和配置zap日志记录器?
英文:
I started using the zap log library for my Go project. I want to print different colors to tty console based on the log level.
I find the zap/internal/color
package can display different colors for strings, but I want to change the log level with different color.
I also want to write the log to some log files with different log level.
How to init and config the zap logger?
答案1
得分: 6
刚遇到了同样的问题,以下是一些启用颜色的代码片段:
config := zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
logger, _ := config.Build()
logger.Info("现在日志应该有颜色了")
参考链接:https://github.com/uber-go/zap/pull/307
英文:
Just met the same issue, and here are some code snippets for enable colors:
config := zap.NewDevelopmentConfig()
config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
logger, _ := config.Build()
logger.Info("Now logs should be colored")
reference: https://github.com/uber-go/zap/pull/307
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论