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.
51 lines
1.2 KiB
51 lines
1.2 KiB
2 years ago
|
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: Magenta,
|
||
|
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] = append(_levelToLowercaseColorString[level], color.Add(level.String()), color.Add("caller"))
|
||
|
_levelToCapitalColorString[level] = append(_levelToCapitalColorString[level], color.Add(level.CapitalString()), color.Add("caller"))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// 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)
|
||
|
}
|