作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢!
一个代码使用了官方的日志库“golang.org/x/exp/slog“,发现两个奇葩点:
1.设置日志级别要这么写:
slogger.Enabled(context.Background(), slog.LevelDebug)
2.日志级别最小只能是 Info,不能是更小的 Debug:
源码 golang.org/x/exp@v0.0.0-20230510235704-dd950f8aeaea/slog/handler.go:214
// enabled reports whether l is greater than or equal to the
// minimum level.
func (h *commonHandler) enabled(l Level) bool {
minLevel := LevelInfo // 默认了最小级别就是 info
if h.opts.Level != nil {
minLevel = h.opts.Level.Level()
}
return l >= minLevel
}
/*
const (
LevelDebug Level = -4
LevelInfo Level = 0
LevelWarn Level = 4
LevelError Level = 8
)
*/
看来还是使用流行的成熟的日志库吧……