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?

huangapple go评论93阅读模式
英文:

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

huangapple
  • 本文由 发表于 2017年3月31日 00:49:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/43123871.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定