log/level_color.go

56 lines
931 B
Go
Raw Normal View History

2019-11-05 07:57:09 +00:00
package log
import (
"fmt"
"go.uber.org/zap/zapcore"
)
var (
_levelToColor = map[zapcore.Level]Color{
zapcore.DebugLevel: Magenta,
zapcore.InfoLevel: Blue,
zapcore.WarnLevel: Yellow,
zapcore.ErrorLevel: Red,
zapcore.DPanicLevel: Red,
zapcore.PanicLevel: Red,
zapcore.FatalLevel: Red,
}
_unknownLevelColor = Red
_levelToColorStrings = make(map[zapcore.Level]string, len(_levelToColor))
)
type Color uint8
//noinspection GoUnusedConst
const (
Black Color = iota + 30
Red
Green
Yellow
Blue
Magenta
Cyan
White
)
2020-12-31 08:21:31 +00:00
func (config *logConfig) initColor() {
2019-11-05 07:57:09 +00:00
for level, color := range _levelToColor {
lcs := level.String()
2020-12-31 08:21:31 +00:00
if config.EnableCapitalLevel {
2019-11-05 07:57:09 +00:00
lcs = level.CapitalString()
}
2020-12-31 08:21:31 +00:00
if config.EnableLevelTruncation {
2019-11-05 07:57:09 +00:00
lcs = lcs[:4]
}
_levelToColorStrings[level] = color.Add(lcs)
}
}
func (c Color) Add(s string) string {
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c), s)
}