I use zap I've set the log level as a debug level but when I run the application I get all levels.
cfg := zap.Config{
Encoding: "json",
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
EncodeLevel: zapcore.CapitalLevelEncoder,
TimeKey: "time",
EncodeTime: zapcore.ISO8601TimeEncoder,
CallerKey: "caller",
EncodeCaller: zapcore.ShortCallerEncoder,
},
}
logger, err := cfg.Build()
if err != nil {
panic(err)
}
defer logger.Sync()
logger.Info("info msg")
logger.Debug("debug msg")
logger.Error("error msg")
How can I set a specific debug level?
Setting the log level to
DEBUG
will log everything withDEBUG
level and above (which includesINFO
andERROR
), it won't log only debug level logs. Logging is a hierarchical system; you set the level to the lowest level log you want to see, and then that level and above will be logged.See also
zapcore.Level.Enabled
: