forked from phuslu/log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.go
220 lines (197 loc) · 5.38 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
package log
import (
"fmt"
"io"
"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 a colorized, human-friendly format to Writer.
// IMPORTANT: Don't use ConsoleWriter on critical path of a high concurrency and low latency application.
//
// Default output format:
// {Time} {Level} {Goid} {Caller} > {Message} {Key}={Value} {Key}={Value}
//
// Note: The performance of ConsoleWriter is not good enough, because it will
// parses JSON input into structured records, then output in a specific order.
// Roughly 2x faster than logrus.TextFormatter, 0.8x fast as zap.ConsoleEncoder,
// and 5x faster than zerolog.ConsoleWriter.
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
// Formatter specifies an optional text formatter for creating a customized output,
// If it is set, ColorOutput, QuoteString and EndWithMessage will be ignore.
Formatter func(w io.Writer, args *FormatterArgs) (n int, err error)
// 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) (int, error) {
b := bbpool.Get().(*bb)
b.B = b.B[:0]
defer bbpool.Put(b)
b.B = append(b.B, p...)
var args FormatterArgs
parseFormatterArgs(b.B, &args)
switch {
case args.Time == "":
return out.Write(p)
case w.Formatter != nil:
return w.Formatter(out, &args)
default:
return w.format(out, &args)
}
}
func (w *ConsoleWriter) format(out io.Writer, args *FormatterArgs) (n int, err error) {
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"
)
// colorful level string
var color, three string
switch args.Level {
case "trace":
color, three = Magenta, "TRC"
case "debug":
color, three = Yellow, "DBG"
case "info":
color, three = Green, "INF"
case "warn":
color, three = Red, "WRN"
case "error":
color, three = Red, "ERR"
case "fatal":
color, three = Red, "FTL"
case "panic":
color, three = Red, "PNC"
default:
color, three = Gray, "???"
}
// pretty console writer
if w.ColorOutput {
// header
fmt.Fprintf(b, "%s%s%s %s%s%s ", Gray, args.Time, Reset, color, three, Reset)
if args.Caller != "" {
fmt.Fprintf(b, "%s %s %s>%s", args.Goid, args.Caller, Cyan, Reset)
} else {
fmt.Fprintf(b, "%s>%s", Cyan, Reset)
}
if !w.EndWithMessage {
fmt.Fprintf(b, " %s", args.Message)
}
// key and values
for _, kv := range args.KeyValues {
if w.QuoteString && kv.ValueType == 's' {
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, args.Message)
}
} else {
// header
fmt.Fprintf(b, "%s %s ", args.Time, three)
if args.Caller != "" {
fmt.Fprintf(b, "%s %s >", args.Goid, args.Caller)
} else {
fmt.Fprint(b, ">")
}
if !w.EndWithMessage {
fmt.Fprintf(b, " %s", args.Message)
}
// key and values
for _, kv := range args.KeyValues {
if w.QuoteString && kv.ValueType == 's' {
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", args.Message)
}
}
// stack
if args.Stack != "" {
b.B = append(b.B, '\n')
b.B = append(b.B, args.Stack...)
if args.Stack[len(args.Stack)-1] != '\n' {
b.B = append(b.B, '\n')
}
} else {
b.B = append(b.B, '\n')
}
return out.Write(b.B)
}
type LogfmtFormatter struct {
TimeField string
}
func (f LogfmtFormatter) Formatter(out io.Writer, args *FormatterArgs) (n int, err error) {
b := bbpool.Get().(*bb)
b.B = b.B[:0]
defer bbpool.Put(b)
fmt.Fprintf(b, "%s=%s ", f.TimeField, args.Time)
if args.Level != "" && args.Level[0] != '?' {
fmt.Fprintf(b, "level=%s ", args.Level)
}
if args.Caller != "" {
fmt.Fprintf(b, "goid=%s caller=%s ", args.Goid, strconv.Quote(args.Caller))
}
if args.Stack != "" {
fmt.Fprintf(b, "stack=%s ", strconv.Quote(args.Stack))
}
// key and values
for _, kv := range args.KeyValues {
switch kv.ValueType {
case 't':
fmt.Fprintf(b, "%s ", kv.Key)
case 'f':
fmt.Fprintf(b, "%s=false ", kv.Key)
case 'n':
fmt.Fprintf(b, "%s=%s ", kv.Key, kv.Value)
case 'S':
fmt.Fprintf(b, "%s=%s ", kv.Key, kv.Value)
case 's':
fmt.Fprintf(b, "%s=%s ", kv.Key, strconv.Quote(kv.Value))
default:
fmt.Fprintf(b, "%s=%s ", kv.Key, strconv.Quote(kv.Value))
}
}
// message
fmt.Fprintf(b, "%s\n", strconv.Quote(args.Message))
return out.Write(b.B)
}
var _ Writer = (*ConsoleWriter)(nil)