wangchuxiao f4020a2508 zap
2023-04-18 10:45:23 +08:00

51 lines
1.0 KiB
Go

package log
import (
"fmt"
"go.uber.org/zap/zapcore"
)
// Foreground colors.
const (
Black Color = iota + 30
Red
Green
Yellow
Blue
Magenta
Cyan
White
)
var (
_levelToColor = map[zapcore.Level]Color{
zapcore.DebugLevel: White,
zapcore.InfoLevel: Blue,
zapcore.WarnLevel: Yellow,
zapcore.ErrorLevel: Red,
zapcore.DPanicLevel: Red,
zapcore.PanicLevel: Red,
zapcore.FatalLevel: Red,
}
_unknownLevelColor = make(map[zapcore.Level]string, len(_levelToColor))
_levelToLowercaseColorString = make(map[zapcore.Level]string, len(_levelToColor))
_levelToCapitalColorString = make(map[zapcore.Level]string, len(_levelToColor))
)
func init() {
for level, color := range _levelToColor {
_levelToLowercaseColorString[level] = color.Add(level.String())
_levelToCapitalColorString[level] = color.Add(level.CapitalString())
}
}
// Color represents a text color.
type Color uint8
// Add adds the coloring to the given string.
func (c Color) Add(s string) string {
return fmt.Sprintf("\x1b[%dm%s\x1b[0m", uint8(c), s)
}