-
Notifications
You must be signed in to change notification settings - Fork 42
/
console.go
209 lines (185 loc) · 4.02 KB
/
console.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package log
import (
"bytes"
"encoding/json"
"fmt"
"os"
"runtime"
"strconv"
)
// IsTerminal returns whether the given file descriptor is a terminal.
func IsTerminal(fd uintptr) bool {
return isTerminal(fd, runtime.GOOS, runtime.GOARCH)
}
// ConsoleWriter parses the JSON input and writes it in an
// (optionally) colorized, human-friendly format to os.Stderr
type ConsoleWriter struct {
// ColorOutput determines if used colorized output.
ColorOutput bool
// Deprecated: Use ColorOutput instead.
ANSIColor bool
// QuoteString determines if quoting string values.
QuoteString bool
// EndWithMessage determines if output message in the end.
EndWithMessage bool
// TimeField specifies the time filed name of output message.
TimeField string
}
func (w *ConsoleWriter) write(p []byte) (n int, err error) {
const (
Reset = "\x1b[0m"
Red = "\x1b[31m"
Green = "\x1b[32m"
Yellow = "\x1b[33m"
Cyan = "\x1b[36m"
DarkGray = "\x1b[90m"
)
var m map[string]interface{}
decoder := json.NewDecoder(bytes.NewReader(p))
decoder.UseNumber()
err = decoder.Decode(&m)
if err != nil {
n, err = os.Stderr.Write(p)
return
}
b := bbpool.Get().(*bb)
b.Reset()
defer bbpool.Put(b)
var timeField = w.TimeField
if timeField == "" {
timeField = "time"
}
if v, ok := m[timeField]; ok {
if w.ColorOutput || w.ANSIColor {
fmt.Fprintf(b, "%s%s%s ", DarkGray, v, Reset)
} else {
fmt.Fprintf(b, "%s ", v)
}
}
if v, ok := m["level"]; ok {
var c, s string
switch s, _ = v.(string); ParseLevel(s) {
case DebugLevel:
c, s = Yellow, "DBG"
case InfoLevel:
c, s = Green, "INF"
case WarnLevel:
c, s = Red, "WRN"
case ErrorLevel:
c, s = Red, "ERR"
case FatalLevel:
c, s = Red, "FTL"
default:
c, s = Yellow, "???"
}
if w.ColorOutput || w.ANSIColor {
fmt.Fprintf(b, "%s%s%s ", c, s, Reset)
} else {
fmt.Fprintf(b, "%s ", s)
}
}
if v, ok := m["caller"]; ok {
fmt.Fprintf(b, "%s ", v)
}
var msgField = "message"
if _, ok := m[msgField]; !ok {
if _, ok := m["msg"]; ok {
msgField = "msg"
}
}
if v, ok := m[msgField]; ok && !w.EndWithMessage {
if s, _ := v.(string); s != "" && s[len(s)-1] == '\n' {
v = s[:len(s)-1]
}
if w.ColorOutput || w.ANSIColor {
fmt.Fprintf(b, "%s>%s %s", Cyan, Reset, v)
} else {
fmt.Fprintf(b, "> %s", v)
}
} else {
if w.ColorOutput || w.ANSIColor {
fmt.Fprintf(b, "%s>%s", Cyan, Reset)
} else {
fmt.Fprint(b, ">")
}
}
for _, k := range jsonKeys(p) {
switch k {
case timeField, msgField, "level", "caller", "stack":
continue
}
v := m[k]
if w.QuoteString {
if s, ok := v.(string); ok {
v = strconv.Quote(s)
}
}
if w.ColorOutput || w.ANSIColor {
if k == "error" && v != nil {
fmt.Fprintf(b, " %s%s=%v%s", Red, k, v, Reset)
} else {
fmt.Fprintf(b, " %s%s=%s%v%s", Cyan, k, DarkGray, v, Reset)
}
} else {
fmt.Fprintf(b, " %s=%v", k, v)
}
}
if w.EndWithMessage {
if v, ok := m[msgField]; ok {
if s, _ := v.(string); s != "" && s[len(s)-1] == '\n' {
v = s[:len(s)-1]
}
if w.ColorOutput || w.ANSIColor {
fmt.Fprintf(b, "%s %s", Reset, v)
} else {
fmt.Fprintf(b, " %s", v)
}
}
}
if v, ok := m["stack"]; ok {
b.B = append(b.B, '\n')
if s, ok := v.(string); ok {
b.B = append(b.B, s...)
} else {
data, _ := json.MarshalIndent(v, "", " ")
b.B = append(b.B, data...)
}
}
b.B = append(b.B, '\n')
return os.Stderr.Write(b.B)
}
func jsonKeys(data []byte) (keys []string) {
var depth, count int
decoder := json.NewDecoder(bytes.NewReader(data))
for {
token, err := decoder.Token()
if err != nil {
break
}
// fmt.Printf("==== %d %d <%T> %v\n", depth, count, token, token)
switch token.(type) {
case json.Delim:
switch token.(json.Delim) {
case '{', '[':
depth++
case '}', ']':
depth--
if depth == 1 {
count++
}
}
case string:
if depth == 1 {
if count%2 == 0 {
keys = append(keys, token.(string))
}
count++
}
default:
if depth == 1 {
count++
}
}
}
return
}