feat: logx with color (#1872)
* feat: logx with color * chore: update logs * fix test error * chore: change colors of http codes * chore: add comments * chore: use faith/color instead of ascii code color * chore: update colors * chore: update colors * chore: fix duplicated slowcall text * chore: remove slowcall colors
This commit is contained in:
@@ -10,6 +10,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/color"
|
||||
)
|
||||
|
||||
type (
|
||||
@@ -239,6 +241,7 @@ func output(writer io.Writer, level string, val interface{}, fields ...LogField)
|
||||
|
||||
switch atomic.LoadUint32(&encoding) {
|
||||
case plainEncodingType:
|
||||
level = wrapLevelWithColor(level)
|
||||
writePlainAny(writer, level, val, buildFields(fields...)...)
|
||||
default:
|
||||
entry := make(logEntryWithFields)
|
||||
@@ -252,6 +255,30 @@ func output(writer io.Writer, level string, val interface{}, fields ...LogField)
|
||||
}
|
||||
}
|
||||
|
||||
func wrapLevelWithColor(level string) string {
|
||||
var colour color.Color
|
||||
switch level {
|
||||
case levelAlert:
|
||||
colour = color.FgRed
|
||||
case levelError:
|
||||
colour = color.FgRed
|
||||
case levelFatal:
|
||||
colour = color.FgRed
|
||||
case levelInfo:
|
||||
colour = color.FgBlue
|
||||
case levelSlow:
|
||||
colour = color.FgYellow
|
||||
case levelStat:
|
||||
colour = color.FgGreen
|
||||
}
|
||||
|
||||
if colour == color.NoColor {
|
||||
return level
|
||||
}
|
||||
|
||||
return color.WithColorPadding(level, colour)
|
||||
}
|
||||
|
||||
func writeJson(writer io.Writer, info interface{}) {
|
||||
if content, err := json.Marshal(info); err != nil {
|
||||
log.Println(err.Error())
|
||||
|
||||
Reference in New Issue
Block a user