You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
62 lines
1.0 KiB
62 lines
1.0 KiB
package log
|
|
|
|
import (
|
|
"log/slog"
|
|
)
|
|
|
|
type Level int
|
|
|
|
const (
|
|
LevelTrace Level = iota
|
|
LevelDebug // 用于程序调试
|
|
LevelInfo // 用于程序运行
|
|
LevelWarn // 潜在错误或非预期结果
|
|
LevelError // 发生错误,但不影响系统的继续运行
|
|
LevelFatal
|
|
LevelPanic
|
|
LevelOff
|
|
)
|
|
|
|
// 越界取近值
|
|
func (l Level) real() Level {
|
|
return min(LevelOff, max(l, LevelTrace))
|
|
}
|
|
|
|
// Level 实现 slog.Leveler 接口
|
|
func (l Level) Level() slog.Level {
|
|
return slog.Level(20 - int(LevelOff-l.real())*4)
|
|
}
|
|
|
|
func (l Level) slog() slog.Leveler {
|
|
return l.Level()
|
|
}
|
|
|
|
func (l Level) String() string {
|
|
switch l {
|
|
case LevelTrace:
|
|
return "TRACE"
|
|
case LevelDebug:
|
|
return "DEBUG"
|
|
case LevelInfo:
|
|
return "INFO"
|
|
case LevelWarn:
|
|
return "WARN"
|
|
case LevelError:
|
|
return "ERROR"
|
|
case LevelFatal:
|
|
return "FATAL"
|
|
case LevelPanic:
|
|
return "PANIC"
|
|
case LevelOff:
|
|
return "OFF"
|
|
}
|
|
if l < LevelTrace {
|
|
return "TRACE"
|
|
} else {
|
|
return "OFF"
|
|
}
|
|
}
|
|
|
|
func parseSlogLevel(level slog.Level) Level {
|
|
return Level(level/4 + 2).real()
|
|
}
|
|
|