Zap package in Go prints all logs even I set log level as Debug level

6k Views Asked by At

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

There are 1 best solutions below

0
On

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.