Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions utils/logger/levels.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,33 @@ func ParseLevel(s string) zapcore.Level {
}

func ColorCode(str string, color color.RGBA) string {
r, g, b := color.R, color.G, color.B
return startColor(color) + str + endColor()
}

func ColorToInt(color color.RGBA) (int, int, int, int) {
r, g, b, a := color.R, color.G, color.B, color.A

red, green, blue, alpha := int(r), int(g), int(b), int(a)

return red, green, blue, alpha
}

red, green, blue := int(r), int(g), int(b)
func startColor(color color.RGBA) string {
red, green, blue, alpha := ColorToInt(color)

mode := "38;2;"

if alpha >= 255 {
mode = "48;2;"
}

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

return "\x1b[38;2;" + colorStr + "m" + str + "\x1b[0m"
return "\x1b[" + mode + colorStr + "m"
}

func endColor() string {
return "\x1b[0m"
}

func LevelString(l zapcore.Level) string {
Expand Down
11 changes: 8 additions & 3 deletions utils/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,14 @@ func Format(data ...any) string {
case int:
res += strconv.Itoa(value)
default:
res += "\n" + ColorCode(jsonutils.Pretty(value), color.RGBA{
R: 0, G: 215, B: 135,
})
lines := strings.Split(jsonutils.Pretty(value), "\n")

lineStr := ""

for _, line := range lines {
lineStr += "\n" + startColor(color.RGBA{ R: 0, G: 135, B: 95,}) + line + endColor()
}
res += lineStr
}
}

Expand Down