聊聊zerolog的Level

2021-01-12 14:55:02 浏览数 (1)

本文主要研究一下zerolog的Level

Level

github.com/rs/zerolog@v1.20.0/log.go

代码语言:javascript复制
// Level defines log levels.
type Level int8

const (
    // DebugLevel defines debug log level.
    DebugLevel Level = iota
    // InfoLevel defines info log level.
    InfoLevel
    // WarnLevel defines warn log level.
    WarnLevel
    // ErrorLevel defines error log level.
    ErrorLevel
    // FatalLevel defines fatal log level.
    FatalLevel
    // PanicLevel defines panic log level.
    PanicLevel
    // NoLevel defines an absent log level.
    NoLevel
    // Disabled disables the logger.
    Disabled

    // TraceLevel defines trace log level.
    TraceLevel Level = -1
)

func (l Level) String() string {
    switch l {
    case TraceLevel:
        return "trace"
    case DebugLevel:
        return "debug"
    case InfoLevel:
        return "info"
    case WarnLevel:
        return "warn"
    case ErrorLevel:
        return "error"
    case FatalLevel:
        return "fatal"
    case PanicLevel:
        return "panic"
    case NoLevel:
        return ""
    }
    return ""
}

Level为int8类型,其中trace最小为-1,之后是debug、info、warn、error、fatal、panic、NoLevel及Disabled

log.Level

github.com/rs/zerolog@v1.20.0/log.go

代码语言:javascript复制
func Trace() *zerolog.Event {
    return Logger.Trace()
}

func Debug() *zerolog.Event {
    return Logger.Debug()
}

func Info() *zerolog.Event {
    return Logger.Info()
}

func Warn() *zerolog.Event {
    return Logger.Warn()
}

func Error() *zerolog.Event {
    return Logger.Error()
}

func Fatal() *zerolog.Event {
    return Logger.Fatal()
}

func Panic() *zerolog.Event {
    return Logger.Panic()
}

log.go提供了Trace、Debug、Info、Warn、Error、Fatal、Panic方法来创建*zerolog.Event

logger.newEvent

github.com/rs/zerolog@v1.20.0/log.go

代码语言:javascript复制
func (l *Logger) Trace() *Event {
    return l.newEvent(TraceLevel, nil)
}

func (l *Logger) Debug() *Event {
    return l.newEvent(DebugLevel, nil)
}

func (l *Logger) Warn() *Event {
    return l.newEvent(WarnLevel, nil)
}

func (l *Logger) Error() *Event {
    return l.newEvent(ErrorLevel, nil)
}

func (l *Logger) Fatal() *Event {
    return l.newEvent(FatalLevel, func(msg string) { os.Exit(1) })
}

func (l *Logger) Panic() *Event {
    return l.newEvent(PanicLevel, func(msg string) { panic(msg) })
}

func (l *Logger) newEvent(level Level, done func(string)) *Event {
    enabled := l.should(level)
    if !enabled {
        return nil
    }
    e := newEvent(l.w, level)
    e.done = done
    e.ch = l.hooks
    if level != NoLevel {
        e.Str(LevelFieldName, LevelFieldMarshalFunc(level))
    }
    if l.context != nil && len(l.context) > 1 {
        e.buf = enc.AppendObjectData(e.buf, l.context)
    }
    return e
}

newEvent方法首先判断当前logger是否允许指定的level,不允许则直接返回nil;允许的话则通过event包的newEvent来新建event,设置done、hooks

event.newEvent

github.com/rs/zerolog@v1.20.0/event.go

代码语言:javascript复制
func newEvent(w LevelWriter, level Level) *Event {
    e := eventPool.Get().(*Event)
    e.buf = e.buf[:0]
    e.ch = nil
    e.buf = enc.AppendBeginMarker(e.buf)
    e.w = w
    e.level = level
    e.stack = false
    return e
}

newEvent方法从eventPool获取一个Event,然后重置一下其属性

WithLevel

github.com/rs/zerolog@v1.20.0/log/log.go

代码语言:javascript复制
func WithLevel(level zerolog.Level) *zerolog.Event {
    return Logger.WithLevel(level)
}

func (l *Logger) WithLevel(level Level) *Event {
    switch level {
    case TraceLevel:
        return l.Trace()
    case DebugLevel:
        return l.Debug()
    case InfoLevel:
        return l.Info()
    case WarnLevel:
        return l.Warn()
    case ErrorLevel:
        return l.Error()
    case FatalLevel:
        return l.newEvent(FatalLevel, nil)
    case PanicLevel:
        return l.newEvent(PanicLevel, nil)
    case NoLevel:
        return l.Log()
    case Disabled:
        return nil
    default:
        panic("zerolog: WithLevel(): invalid level: "   strconv.Itoa(int(level)))
    }
}

WithLevel可以动态传入Level,然后返回指定level的event

SetGlobalLevel

github.com/rs/zerolog@v1.20.0/ctx.go

代码语言:javascript复制
func init() {
    SetGlobalLevel(TraceLevel)
    l := Nop()
    disabledLogger = &l
}

默认是TraceLevel

实例

代码语言:javascript复制
func withLevelDemo() {
    log.WithLevel(zerolog.TraceLevel).Msg("trace level")
    log.WithLevel(zerolog.DebugLevel).Msg("debug level")
    log.WithLevel(zerolog.InfoLevel).Msg("info level")
}

输出

代码语言:javascript复制
{"level":"trace","time":"2021-01-03T20:59:42 08:00","message":"trace level"}
{"level":"debug","time":"2021-01-03T20:59:42 08:00","message":"debug level"}
{"level":"info","time":"2021-01-03T20:59:42 08:00","message":"info level"}

小结

Level为int8类型,其中trace最小为-1,之后是debug、info、warn、error、fatal、panic、NoLevel及Disabled;log.go提供了Trace、Debug、Info、Warn、Error、Fatal、Panic方法来创建*zerolog.Event;WithLevel可以动态传入Level,然后返回指定level的event。

doc

  • zerolog

0 人点赞