序
本文主要研究一下golang的zap的level
Level[email protected]/zapcore/level.go
// A Level is a logging priority. Higher levels are more important.type Level int8const ( // 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 _minLevel = DebugLevel _maxLevel = FatalLevel)func (l Level) String() string { switch l { case DebugLevel: return "debug" case InfoLevel: return "info" case WarnLevel: return "warn" case ErrorLevel: return "error" case DPanicLevel: return "dpanic" case PanicLevel: return "panic" case FatalLevel: return "fatal" default: return fmt.Sprintf("Level(%d)", l) }}
Level為int8型別,其中DebugLevel值最小,FatalLevel值最大
LevelEnabler[email protected]/zapcore/level.go
type LevelEnabler interface { Enabled(Level) bool}func (l Level) Enabled(lvl Level) bool { return lvl >= l}
LevelEnabler介面定義了Enabled方法,Level的Enabled方法判斷lvl是否大於等於l
levelToColor[email protected]/zapcore/level_strings.go
import "go.uber.org/zap/internal/color"var ( _levelToColor = map[Level]color.Color{ DebugLevel: color.Magenta, InfoLevel: color.Blue, WarnLevel: color.Yellow, ErrorLevel: color.Red, DPanicLevel: color.Red, PanicLevel: color.Red, FatalLevel: color.Red, } _unknownLevelColor = color.Red _levelToLowercaseColorString = make(map[Level]string, len(_levelToColor)) _levelToCapitalColorString = make(map[Level]string, len(_levelToColor)))func init() { for level, color := range _levelToColor { _levelToLowercaseColorString[level] = color.Add(level.String()) _levelToCapitalColorString[level] = color.Add(level.CapitalString()) }}
level_strings定義了_levelToColor的對映,其中DebugLevel為color.Magenta,InfoLevel為color.Blue,WarnLevel為color.Yellow,其餘的為color.Red
levelFilterCore[email protected]/zapcore/increase_level.go
type levelFilterCore struct { core Core level LevelEnabler}// NewIncreaseLevelCore creates a core that can be used to increase the level of// an existing Core. It cannot be used to decrease the logging level, as it acts// as a filter before calling the underlying core. If level decreases the log level,// an error is returned.func NewIncreaseLevelCore(core Core, level LevelEnabler) (Core, error) { for l := _maxLevel; l >= _minLevel; l-- { if !core.Enabled(l) && level.Enabled(l) { return nil, fmt.Errorf("invalid increase level, as level %q is allowed by increased level, but not by existing core", l) } } return &levelFilterCore{core, level}, nil}func (c *levelFilterCore) Enabled(lvl Level) bool { return c.level.Enabled(lvl)}func (c *levelFilterCore) With(fields []Field) Core { return &levelFilterCore{c.core.With(fields), c.level}}func (c *levelFilterCore) Check(ent Entry, ce *CheckedEntry) *CheckedEntry { if !c.Enabled(ent.Level) { return ce } return c.core.Check(ent, ce)}func (c *levelFilterCore) Write(ent Entry, fields []Field) error { return c.core.Write(ent, fields)}func (c *levelFilterCore) Sync() error { return c.core.Sync()}
levelFilterCore定義了Core、LevelEnabler屬性;其Check方法首先透過c.Enabled(ent.Level)判斷entry的level是否大於等於core的level,如果滿足條件才執行c.core.Check(ent, ce),否則直接返回
IncreaseLevel[email protected]/options.go
func IncreaseLevel(lvl zapcore.LevelEnabler) Option { return optionFunc(func(log *Logger) { core, err := zapcore.NewIncreaseLevelCore(log.core, lvl) if err != nil { fmt.Fprintf(log.errorOutput, "failed to IncreaseLevel: %v\n", err) } else { log.core = core } })}
IncreaseLevel會透過zapcore.NewIncreaseLevelCore建立一個新的core,它包裝了原來的core,並設定了zapcore.LevelEnabler
例項func levelDemo() { logger, err := zap.NewDevelopment() defer logger.Sync() if err != nil { panic(err) } infoLog := logger.WithOptions(zap.IncreaseLevel(zapcore.InfoLevel)) logger.Debug("this is debug log") infoLog.Debug("this will be dropped")}
輸出
2020-12-21T22:44:12.385+0800 DEBUG zap/zap_demo.go:29 this is debug log
小結
Level為int8型別,其中DebugLevel值最小,FatalLevel值最大;LevelEnabler介面定義了Enabled方法,Level的Enabled方法判斷lvl是否大於等於l;levelFilterCore定義了Core、LevelEnabler屬性;其Check方法首先透過c.Enabled(ent.Level)判斷entry的level是否大於等於core的level,如果滿足條件才執行c.core.Check(ent, ce),否則直接返回。
doczap