-
Notifications
You must be signed in to change notification settings - Fork 42
/
console.go
278 lines (253 loc) · 7.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package log
import (
"errors"
"fmt"
"io"
"path"
"runtime"
"strconv"
"strings"
"text/template"
)
// 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 Writer.
//
// Default output format:
// {Time} {Level} {Goid} {Caller} > {Message} {Key}={Value} {Key}={Value}
//
// Note: ConsoleWriter performance is not good, it will parses JSON input into
// structured records, then outputs them in a specific order.
type ConsoleWriter struct {
// ColorOutput determines if used colorized output.
ColorOutput bool
// QuoteString determines if quoting string values.
QuoteString bool
// EndWithMessage determines if output message in the end.
EndWithMessage bool
// Template specifies an optional text/template for creating a
// user-defined output format, available arguments are:
// type . struct {
// Time string // "2019-07-10T05:35:54.277Z"
// Level Level // InfoLevel
// Caller string // "prog.go:42"
// Goid string // "123"
// Message string // "a structure message"
// Stack string // "<stack string>"
// KeyValue []struct {
// Key string // "foo"
// Value string // "bar"
// }
// }
// See https://github.com/phuslu/log#template-console-writer for example.
//
// If Template is not nil, ColorOutput, QuoteString and EndWithMessage are override.
Template *template.Template
// Writer is the output destination. using os.Stderr if empty.
Writer io.Writer
}
// Close implements io.Closer, will closes the underlying Writer if not empty.
func (w *ConsoleWriter) Close() (err error) {
if w.Writer != nil {
if closer, ok := w.Writer.(io.Closer); ok {
err = closer.Close()
}
}
return
}
func (w *ConsoleWriter) write(out io.Writer, p []byte) (n int, err error) {
var t dot
err = jsonToDot(p, &t)
if err != nil {
n, err = out.Write(p)
return
}
b := bbpool.Get().(*bb)
b.B = b.B[:0]
defer bbpool.Put(b)
const (
Reset = "\x1b[0m"
Black = "\x1b[30m"
Red = "\x1b[31m"
Green = "\x1b[32m"
Yellow = "\x1b[33m"
Blue = "\x1b[34m"
Magenta = "\x1b[35m"
Cyan = "\x1b[36m"
White = "\x1b[37m"
Gray = "\x1b[90m"
)
// template console writer
if w.Template != nil {
w.Template.Execute(b, &t)
if len(b.B) > 0 && b.B[len(b.B)-1] != '\n' {
b.B = append(b.B, '\n')
}
return out.Write(b.B)
}
// pretty console writer
if w.ColorOutput {
// colorful level string
var levelColor = Gray
switch t.Level {
case TraceLevel:
levelColor = Magenta
case DebugLevel:
levelColor = Yellow
case InfoLevel:
levelColor = Green
case WarnLevel:
levelColor = Red
case ErrorLevel:
levelColor = Red
case FatalLevel:
levelColor = Red
case PanicLevel:
levelColor = Red
}
// header
fmt.Fprintf(b, "%s%s%s %s%s%s ", Gray, t.Time, Reset, levelColor, t.Level.Three(), Reset)
if t.Caller != "" {
fmt.Fprintf(b, "%s %s %s>%s", t.Goid, t.Caller, Cyan, Reset)
} else {
fmt.Fprintf(b, "%s>%s", Cyan, Reset)
}
if !w.EndWithMessage {
fmt.Fprintf(b, " %s", t.Message)
}
// key and values
for _, kv := range t.KeyValue {
if w.QuoteString {
kv.Value = strconv.Quote(kv.Value)
}
if kv.Key == "error" {
fmt.Fprintf(b, " %s%s=%s%s", Red, kv.Key, kv.Value, Reset)
} else {
fmt.Fprintf(b, " %s%s=%s%s%s", Cyan, kv.Key, Gray, kv.Value, Reset)
}
}
// message
if w.EndWithMessage {
fmt.Fprintf(b, "%s %s", Reset, t.Message)
}
} else {
// header
fmt.Fprintf(b, "%s %s ", t.Time, t.Level.Three())
if t.Caller != "" {
fmt.Fprintf(b, "%s %s >", t.Goid, t.Caller)
} else {
fmt.Fprint(b, ">")
}
if !w.EndWithMessage {
fmt.Fprintf(b, " %s", t.Message)
}
// key and values
for _, kv := range t.KeyValue {
if w.QuoteString {
fmt.Fprintf(b, " %s=%s", kv.Key, strconv.Quote(kv.Value))
} else {
fmt.Fprintf(b, " %s=%s", kv.Key, kv.Value)
}
}
// message
if w.EndWithMessage {
fmt.Fprintf(b, " %s", t.Message)
}
}
// stack
if t.Stack != "" {
b.B = append(b.B, '\n')
b.B = append(b.B, t.Stack...)
}
b.B = append(b.B, '\n')
return out.Write(b.B)
}
var errInvalidJson = errors.New("invalid json object")
func jsonToDot(json []byte, t *dot) error {
items, err := jsonParse(json)
if err != nil {
return err
}
if len(items) <= 1 {
return errInvalidJson
}
t.Time = b2s(items[1].Value)
for i := 2; i < len(items); i += 2 {
k, v := items[i].Value, items[i+1].Value
switch b2s(k) {
case "level":
t.Level = noLevel
if len(v) > 0 {
t.Level = ParseLevelByte(v[0])
}
case "goid":
t.Goid = b2s(v)
case "caller":
t.Caller = b2s(v)
case "message", "msg":
if len(v) != 0 && v[len(v)-1] == '\n' {
t.Message = b2s(v[:len(v)-1])
} else {
t.Message = b2s(v)
}
case "stack":
t.Stack = b2s(v)
default:
t.KeyValue = append(t.KeyValue, dotkv{b2s(k), b2s(v)})
}
}
return nil
}
type dot struct {
Time string
Level Level
Caller string
Goid string
Message string
Stack string
KeyValue []dotkv
}
type dotkv struct {
Key string
Value string
}
// ColorTemplate provides a pre-defined text/template for console color output
// Note: use [sprig](https://github.com/Masterminds/sprig) to introduce more template functions.
const ColorTemplate = `{{gray .Time -}}
{{if eq .Level -1 }}{{magenta " TRC " -}}
{{else if eq .Level 0 }}{{yellow " DBG " -}}
{{else if eq .Level 1}}{{green " INF " -}}
{{else if eq .Level 2}}{{red " WRN " -}}
{{else if eq .Level 3}}{{red " ERR " -}}
{{else if eq .Level 4}}{{red " FTL " -}}
{{else if eq .Level 5}}{{red " PNC " -}}
{{else}}{{red " ??? "}}{{end -}}
{{.Goid}} {{.Caller}}{{cyan " >" -}}
{{range .KeyValue -}}
{{if eq .Key "error" }} {{red (printf "%s%s%s" .Key "=" .Value) -}}
{{else}} {{cyan .Key}}={{gray .Value}}{{end -}}
{{end}} {{.Message}}
{{.Stack}}`
// ColorFuncMap provides a pre-defined template functions for color string
var ColorFuncMap = template.FuncMap{
"black": func(s string) string { return "\x1b[30m" + s + "\x1b[0m" },
"red": func(s string) string { return "\x1b[31m" + s + "\x1b[0m" },
"green": func(s string) string { return "\x1b[32m" + s + "\x1b[0m" },
"yellow": func(s string) string { return "\x1b[33m" + s + "\x1b[0m" },
"blue": func(s string) string { return "\x1b[34m" + s + "\x1b[0m" },
"magenta": func(s string) string { return "\x1b[35m" + s + "\x1b[0m" },
"cyan": func(s string) string { return "\x1b[36m" + s + "\x1b[0m" },
"white": func(s string) string { return "\x1b[37m" + s + "\x1b[0m" },
"gray": func(s string) string { return "\x1b[90m" + s + "\x1b[0m" },
"contains": strings.Contains,
"endswith": strings.HasSuffix,
"lower": strings.ToLower,
"match": path.Match,
"quote": strconv.Quote,
"startswith": strings.HasPrefix,
"title": strings.ToTitle,
"upper": strings.ToUpper,
}