7

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?

blackgreen
  • 34,072
  • 23
  • 111
  • 129
savior
  • 802
  • 6
  • 14
  • For how to log to different outputs based on level, see also [this](https://stackoverflow.com/questions/68472667/how-to-log-to-stdout-or-stderr-based-on-log-level-using-uber-go-zap/68476472#68476472) – blackgreen Jul 31 '21 at 11:40

1 Answers1

6

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