英文:
What is the correspondence between go-logr and uber's zap verbosity levels?
问题
我看到Uber Zap实现中有日志级别:
const (
// DebugLevel通常生成大量日志,在生产环境中通常禁用。
DebugLevel Level = iota - 1
// InfoLevel是默认的日志优先级。
InfoLevel
// WarnLevel日志比Info级别更重要,但不需要单独的人工审核。
WarnLevel
// ErrorLevel日志是高优先级的。如果应用程序运行正常,不应生成任何错误级别的日志。
ErrorLevel
// DPanicLevel日志是特别重要的错误。在开发中,记录器在写入消息后会引发panic。
DPanicLevel
// PanicLevel记录一条消息,然后引发panic。
PanicLevel
// FatalLevel记录一条消息,然后调用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
}
// 这里的zap是"sigs.k8s.io/controller-runtime/pkg/log/zap"
opts := zap.Options{
StacktraceLevel: ... ,
Level: determineLogLevel("ERROR"),
Encoder: ... ,
ZapOpts: ...,
}
但也可以使用logr.Logger.V
选项。
这里的级别值是否与Uber Zap的常量(DebugLevel
,InfoLevel
,WarnLevel
等)相同?
我还看到了这个:
flag --zap-log-level:Zap Level用于配置日志的详细程度。可以是'debug'、'info'、'error',或者任何大于0的整数值,对应于递增详细程度的自定义调试级别。
这个标志值是否与sigs.k8s.io
的zap.Options
中的zapcore.Level
相同?
关于logr.Logger.V
的文档:
// V返回与此Logger相对于特定详细程度的Logger值。换句话说,V值是可叠加的。V更高的详细程度意味着日志消息不太重要。传递小于零的日志级别是非法的。
V(level int) Logger
英文:
I saw that there is log level in Uber Zap implementation:
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
)
I use this when I set the level in a sigs.k8s.io/controller-runtime/pkg/log/zap
logger, which uses go-logr
under the hood:
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: ...,
}
But there is also the option of using logr.Logger.V
.
Is the level value here the same as in Uber Zap's constants? ( DebugLevel
, InfoLevel
, WarnLevel
, ....)
I also saw this:
> 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”
Is this flag value the same as zapcore.Level
in the sigs.k8s.io
's zap.Options
?
The documentation for 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
答案1
得分: 2
go-logr
和go.uber.org/zap
日志级别之间的对应关系如下:
zapLevel = -1 * logrLevel
换句话说,go-logr
的级别是zap
级别的相反数。这个信息可以在go-logr/zapr
包的文档中找到:
logr中的级别对应于Zap中的自定义调试级别。在Zap中,logr中的任何给定级别都由其在zap中的相反数表示(
zapLevel = -1*logrLevel
)。例如,V(2)等同于Zap的级别-2,而V(1)等同于Zap的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
使用zap.InfoLevel
(你知道它是0
)构造了一个zapr.zapLogger
,所以每次在这个实现上调用V
时,它会减去给定的整数值,从而得到它的负值。
--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
的实现,根据上述引用的文档要求。
英文:
The correspondence between go-logr
and go.uber.org/zap
log levels is given by:
zapLevel = -1 * logrLevel
In other words, the go-logr
level is the inverse of zap
level. This information is available in go-logr/zapr
package docs:
> 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.
You can also see a concrete example of how the level is initialized by looking at the implementation of logr.Logger.V
by zapr
package:
func (zl *zapLogger) V(level int) logr.Logger {
return &zapLogger{
lvl: zl.lvl - zapcore.Level(level),
l: zl.l,
}
}
The method zapr.NewLogger
constructs a zapr.zapLogger
with lvl
field set to zap.InfoLevel
(which you know is 0
), so each time you call V
on this implementation, it subtracts the given int value, thus obtaining its negative.
<hr>
The flag --zap-log-level
is mapped from the string value passed on the command line (or k8s yaml config) to the Uber Zap's level as-is, based on this:
var levelStrings = map[string]zapcore.Level{
"debug": zap.DebugLevel,
"info": zap.InfoLevel,
"error": zap.ErrorLevel,
}
The numerical value is then multiplied by -1
and then set to the logr.Logger
implementation, as required by the documentation quoted above.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论