关于golang:聊聊zerolog的Level

42次阅读

共计 3690 个字符,预计需要花费 10 分钟才能阅读完成。

本文次要钻研一下 zerolog 的 Level

Level

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

// 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

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

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

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

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

func init() {SetGlobalLevel(TraceLevel)
    l := Nop()
    disabledLogger = &l
}

默认是 TraceLevel

实例

func withLevelDemo() {log.WithLevel(zerolog.TraceLevel).Msg("trace level")
    log.WithLevel(zerolog.DebugLevel).Msg("debug level")
    log.WithLevel(zerolog.InfoLevel).Msg("info level")
}

输入

{"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