我看到 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 的常量相同?( DebugLevel, InfoLevel, WarnLevel, ....)
我也看到了這個:
flag --zap-log-level:Zap 級別以配置日志記錄的詳細程度。可以是 'debug'、'info'、'error' 或任何大于 0 的整數值之一,對應于增加詳細程度的自定義除錯級別”
這是標志值一樣zapcore.Level的sigs.k8s.io的zap.Options?
的檔案 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
uj5u.com熱心網友回復:
go-logr和go.uber.org/zap日志級別之間的對應關系由下式給出:
zapLevel = -1 * logrLevel
換句話說,該go-logr電平是反向的zap電平。此資訊可在go-logr/zapr包檔案中找到:
logr 中的級別對應于 Zap 中的自定義除錯級別。logr 中的任何給定級別都由其在 zap (
zapLevel = -1*logrLevel) 中的倒數表示。例如,V(2) 相當于 Zap 中的日志級別 -2,而 V(1) 相當于 Zap 的 DebugLevel。
您還可以通過查看logr.Logger.Vbyzapr包的實作來查看如何初始化級別的具體示例:
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實作。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/343530.html
上一篇:JQuery學習筆記,加油
