英文:
Zap package in Go prints all logs even I set log level as Debug level
问题
我使用 zap。我已将日志级别设置为调试级别,但当我运行应用程序时,我得到了所有级别的日志。
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")
如何设置特定的调试级别?
英文:
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?
答案1
得分: 4
将日志级别设置为DEBUG
将记录所有具有DEBUG
级别及以上的内容(包括INFO
和ERROR
),它不仅仅记录调试级别的日志。日志记录是一个分层系统;您将级别设置为您想要查看的最低级别日志,然后该级别及以上的日志将被记录。
> 如果给定的级别等于或高于此级别,则返回true。
英文:
Setting the log level to DEBUG
will log everything with DEBUG
level and above (which includes INFO
and ERROR
), 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
:
> Enabled returns true if the given level is at or above this level.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论