sequence

This paper mainly studies the level of GOLang’s ZAP

Level

[email protected] / zapcore/level. Go

// A Level is a logging priority. Higher levels are more important.
type Level int8

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

	_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)
	}
}
Copy the code

The Level type is INT8. The DebugLevel value is the smallest and FatalLevel value is the largest

LevelEnabler

[email protected] / zapcore/level. Go

type LevelEnabler interface {
	Enabled(Level) bool
}

func (l Level) Enabled(lvl Level) bool {
	return lvl >= l
}
Copy the code

The LevelEnabler interface defines the Enabled method. The Enabled method of Level determines whether LVL is greater than or equal to 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())
	}
}
Copy the code

Level_strings defines the mapping of _levelToColor where the DebugLevel is color.Magenta, InfoLevel is color.Blue, WarnLevel is color.Yellow, and the rest are 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() }Copy the code

LevelFilterCore defines the Core and LevelEnabler properties. The Check method checks whether the entry Level is greater than or equal to the Level of the core through c. ennabled (ent.Level). If the condition is met, the Check method executes 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 } }) }Copy the code

Can through zapcore IncreaseLevel NewIncreaseLevelCore to create a new core, it wraps the original core, and setting the zapcore. LevelEnabler

The instance

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") }Copy the code

The output

2020-12-21T22:44:12.385+0800 DEBUG Zap /zap_demo.go:29 This is DEBUG logCopy the code

summary

Level is int8, where DebugLevel is the smallest and FatalLevel is the largest. The LevelEnabler interface defines the Enabled method. The Enabled method of Level determines whether LVL is greater than or equal to L. LevelFilterCore defines the Core and LevelEnabler properties. The Check method checks whether the entry Level is greater than or equal to the Level of the core through c. ennabled (ent.Level). If the condition is met, the Check method executes C. Core. Check(ENT, CE).

doc

  • zap