Skip to content

Commit 598569f

Browse files
authored
fix: pretty print (#110)
1 parent 8129109 commit 598569f

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

utils/logger/levels.go

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,33 @@ func ParseLevel(s string) zapcore.Level {
3030
}
3131

3232
func ColorCode(str string, color color.RGBA) string {
33-
r, g, b := color.R, color.G, color.B
33+
return startColor(color) + str + endColor()
34+
}
35+
36+
func ColorToInt(color color.RGBA) (int, int, int, int) {
37+
r, g, b, a := color.R, color.G, color.B, color.A
38+
39+
red, green, blue, alpha := int(r), int(g), int(b), int(a)
40+
41+
return red, green, blue, alpha
42+
}
3443

35-
red, green, blue := int(r), int(g), int(b)
44+
func startColor(color color.RGBA) string {
45+
red, green, blue, alpha := ColorToInt(color)
46+
47+
mode := "38;2;"
48+
49+
if alpha >= 255 {
50+
mode = "48;2;"
51+
}
3652

3753
colorStr := strconv.Itoa(red) + ";" + strconv.Itoa(green) + ";" + strconv.Itoa(blue)
3854

39-
return "\x1b[38;2;" + colorStr + "m" + str + "\x1b[0m"
55+
return "\x1b[" + mode + colorStr + "m"
56+
}
57+
58+
func endColor() string {
59+
return "\x1b[0m"
4060
}
4161

4262
func LevelString(l zapcore.Level) string {

utils/logger/logger.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,14 @@ func Format(data ...any) string {
6060
case int:
6161
res += strconv.Itoa(value)
6262
default:
63-
res += "\n" + ColorCode(jsonutils.Pretty(value), color.RGBA{
64-
R: 0, G: 215, B: 135,
65-
})
63+
lines := strings.Split(jsonutils.Pretty(value), "\n")
64+
65+
lineStr := ""
66+
67+
for _, line := range lines {
68+
lineStr += "\n" + startColor(color.RGBA{ R: 0, G: 135, B: 95,}) + line + endColor()
69+
}
70+
res += lineStr
6671
}
6772
}
6873

0 commit comments

Comments
 (0)