简体   繁体   English

如何使用go.uber.org/zap lib以不同的日志级别打印不同的颜色,并根据日志级别将日志追加到不同的文件?

[英]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?

I start to use the zap log lib for my go project. 我开始在go项目中使用zap日志库。 I want to print the different color to tty console dependent on the log level. 我想根据日志级别将不同的颜色打印到tty控制台。

I find the zap/internal/color can display different color for string, But I want to change the log level with different color. 我发现zap/internal/color可以为字符串显示不同的颜色,但是我想用不同的颜色更改日志级别。

I also want to write the log to some log files with different log level. 我还想将日志写入具有不同日志级别的某些日志文件中。

How to init and config the zap log? 如何初始化和配置zap日志?

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 参考: https : //github.com/uber-go/zap/pull/307

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM