go - go-logr 和 uber 的 zap 详细级别之间的对应关系是什么?

标签 go logging go-zap

我看到 Uber Zap 实现中有日志级别:

    const (
            // DebugLevel logs are typically voluminous, and are usually disabled in
            // production.
            DebugLevel Level = iota - 1
            // InfoLevel is the default logging priority.
            InfoLevel
            // WarnLevel logs are more important than Info, but don't need individual
            // human review.
            WarnLevel
            // ErrorLevel logs are high-priority. If an application is running smoothly,
            // it shouldn't generate any error-level logs.
            ErrorLevel
            // DPanicLevel logs are particularly important errors. In development the
            // logger panics after writing the message.
            DPanicLevel
            // PanicLevel logs a message, then panics.
            PanicLevel
            // FatalLevel logs a message, then calls os.Exit(1).
            FatalLevel
        ) 

当我在 sigs.k8s.io/controller-runtime/pkg/log/zap 记录器中设置级别时,我会使用它,该记录器在下使用 go-logr引擎盖:

func determineLogLevel(verbosityLevel string) zapcore.Level {
    var zapLevel zapcore.Level
    verbosityLevel = strings.ToLower(verbosityLevel)
    switch verbosityLevel {
    case ERROR:
        zapLevel = zapcore.ErrorLevel
    case WARNING:
        zapLevel = zapcore.WarnLevel
    case INFO:
        zapLevel = zapcore.InfoLevel
    case DEBUG:
        zapLevel = zapcore.DebugLevel
    default:
        zapLevel = zapcore.InfoLevel
    }
    return zapLevel
}

// here zap is "sigs.k8s.io/controller-runtime/pkg/log/zap"
opts := zap.Options{
    StacktraceLevel: ... ,
    Level:           determineLogLevel("ERROR"),
    Encoder:         ... ,
    ZapOpts:         ...,
}

但也可以选择使用logr.Logger.V

此处的级别值与 Uber Zap 常量中的级别值相同吗? (调试级别信息级别警告级别、...)

我也看到了这个:

flag --zap-log-level: Zap Level to configure the verbosity of logging. Can be one of ‘debug’, ‘info’, ‘error’, or any integer value > 0 which corresponds to custom debug levels of increasing verbosity”

此标志值与 sigs.k8s.iozap.Options 中的 zapcore.Level 相同吗?

logr.Logger.V 的文档

    // V returns an Logger value for a specific verbosity level, relative to
    // this Logger.  In other words, V values are additive.  V higher verbosity
    // level means a log message is less important.  It's illegal to pass a log
    // level less than zero.
    V(level int) Logger

最佳答案

go-logrgo.uber.org/zap 日志级别之间的对应关系如下:

zapLevel = -1 * logrLevel

换句话说,go-logr 级别是 zap 级别的。此信息可在 go-logr/zapr 中找到。包文档:

Levels in logr correspond to custom debug levels in Zap. Any given level in logr is represents by its inverse in zap (zapLevel = -1*logrLevel). For example V(2) is equivalent to log level -2 in Zap, while V(1) is equivalent to Zap's DebugLevel.

您还可以通过查看 zapr 包的 logr.Logger.V 实现来了解如何初始化级别的具体示例:

func (zl *zapLogger) V(level int) logr.Logger {
    return &zapLogger{
        lvl: zl.lvl - zapcore.Level(level),
        l:   zl.l,
    }
}

方法zapr.NewLogger构造一个zapr.zapLogger,其中lvl字段设置为zap.InfoLevel(您知道它是 0),因此每次您在此实现上调用 V 时,它都会减去给定的 int 值,从而获得其负数。


标志 --zap-log-level 从命令行(或 k8s yaml 配置)上传递的字符串值按原样映射到 Uber Zap 的级别,基于此:

var levelStrings = map[string]zapcore.Level{
    "debug": zap.DebugLevel,
    "info":  zap.InfoLevel,
    "error": zap.ErrorLevel,
}

然后将数值乘以 -1,然后按照上面引用的文档的要求设置为 logr.Logger 实现。

关于go - go-logr 和 uber 的 zap 详细级别之间的对应关系是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69797671/

相关文章:

multithreading - Go 运行时使用的线程数

c# - azure : I don't see all my Trace log

python - 防止 Python 记录器打印到控制台

Python 日志记录 : disable output to stdout

go - 如何使用 go.uber.org/zap lib 以不同的日志级别打印不同的颜色,并根据日志级别将日志附加到不同的文件?

mysql错误在单独的包中使用时拒绝使用go-sql-driver的用户访问

mysql - 如何使用Golang将值插入decimal(65,0)并从数据库中检索它们?

go - 处理请求后关闭 Web 服务器 - Go

go - Uber Zap 记录器不在日志语句中打印调用者信息

go - 使用zap时如何自定义日志格式?