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:
Kevin Wan
2022-05-07 23:22:39 +08:00
committed by GitHub
parent 5383e29ce6
commit 69c2bad410
13 changed files with 261 additions and 26 deletions

View File

@@ -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())