github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/countlog/spi/color.go (about) 1 package spi 2 3 var coloredNames = map[int]string{} 4 5 func init() { 6 levels := []int{ 7 LevelTraceCall, 8 LevelTrace, 9 LevelDebugCall, 10 LevelDebug, 11 LevelInfoCall, 12 LevelInfo, 13 LevelWarn, 14 LevelError, 15 LevelFatal, 16 } 17 for _, level := range levels { 18 coloredNames[level] = ColoredLevelName(level) 19 //coloredNames[level] = msgfmt.Sprintf("\x1b[{color};1m[{level}]\x1b[0m ", 20 // "color", getColor(level), "level", LevelName(level)) 21 } 22 } 23 24 func ColoredLevelName(level int) string { 25 return coloredNames[level] 26 } 27 28 const ( 29 nocolor = 0 30 black = 30 31 red = 31 32 green = 32 33 yellow = 33 34 blue = 34 35 purple = 35 36 cyan = 36 37 gray = 37 38 ) 39 40 func getColor(level int) int { 41 switch level { 42 case LevelTrace, LevelTraceCall: 43 return cyan 44 case LevelDebug, LevelDebugCall: 45 return blue 46 case LevelInfo, LevelInfoCall: 47 return green 48 case LevelWarn: 49 return yellow 50 case LevelError: 51 return red 52 case LevelFatal: 53 return purple 54 default: 55 return nocolor 56 } 57 }