在Go语言中,即使我将日志级别设置为调试级别,Zap包仍会打印所有日志。

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

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级别及以上的内容(包括INFOERROR),它不仅仅记录调试级别的日志。日志记录是一个分层系统;您将级别设置为您想要查看的最低级别日志,然后该级别及以上的日志将被记录。

另请参阅zapcore.Level.Enabled:

> 如果给定的级别等于或高于此级别,则返回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.

huangapple
  • 本文由 发表于 2022年5月26日 17:35:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/72389725.html
匿名

发表评论

匿名网友

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

确定