即使我将日志级别设置为调试级别,Go 中的 Zap 包也会打印所有日志

Mar*_*lal 0 logging go go-zap

我使用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")
Run Code Online (Sandbox Code Playgroud)

如何设置特定的调试级别?

Nic*_*men 5

将日志级别设置为DEBUG将记录DEBUG级别及以上级别的所有内容(包括INFOERROR),它不会仅记录调试级别日志。日志记录是一个分层系统;您将级别设置为您想要查看的最低级别日志,然后该级别及更高级别将被记录。

也可以看看zapcore.Level.Enabled

如果给定级别等于或高于此级别,则启用返回 true。