-
Notifications
You must be signed in to change notification settings - Fork 5
/
colorize.go
95 lines (82 loc) · 1.97 KB
/
colorize.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//go:build what || whathappens || whatif || whatfunc || whatis || whatpackage
// Based on https://github.com/spotlightpa/almanack/blob/master/pkg/almlog/colorize.go by Carlana Johnson.
package what
import (
"bytes"
"fmt"
"time"
)
const (
reset = "\033[0m"
bold = "\033[1m"
dim = "\033[2m"
standout = "\033[3m"
underscore = "\033[4m"
blink = "\033[5m"
blinkmore = "\033[6m"
invert = "\033[7m"
hide = "\033[8m"
del = "\033[9m"
black = "\033[30m"
red = "\033[31m"
green = "\033[32m"
yellow = "\033[33m"
blue = "\033[34m"
magenta = "\033[35m"
cyan = "\033[36m"
white = "\033[37m"
purple = magenta + bold
)
func colorize(level string, args ...any) string {
// Build the prefix: time, level, func, msg...
logArgs := []any{"time", time.Now().UTC().Format("2006-01-02 15:04:05"), "level", level, "func", funcname(3), "msg", args[0]}
// ...then append the key/value pairs
logArgs = append(logArgs, args[1:]...)
var buf bytes.Buffer
// Iterate over the logArgs and apply formatting
for i := 0; i < len(logArgs); i += 2 {
key := fmt.Sprint(logArgs[i])
val := fmt.Sprint(logArgs[i+1])
keyColor := cyan
valColor := magenta
withKey := true
switch key {
case "time":
withKey = false
valColor = dim
case "level":
withKey = false
switch level {
case "DEBUG":
valColor = dim
case "INFO":
valColor = green
case "WARN":
valColor = yellow
case "ERROR":
valColor = red + bold
}
case "func":
withKey = false
valColor = blue
case "msg":
withKey = false
valColor = white + underscore
case "err":
valColor = red + bold
}
// Coloring for value based on level
if withKey {
// Format as `key=value`
buf.WriteString(keyColor)
buf.WriteString(key)
buf.WriteString(reset)
buf.WriteString("=")
}
buf.WriteString(valColor)
buf.WriteString(val)
buf.WriteString(reset)
buf.WriteString(" ")
}
return buf.String()
}