-
Notifications
You must be signed in to change notification settings - Fork 4
/
encoder.go
365 lines (325 loc) · 8.76 KB
/
encoder.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package prettyconsole
import (
"bytes"
"io"
"os"
"sort"
"strconv"
"strings"
"time"
"unicode/utf8"
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
var _ = zap.RegisterEncoder("pretty_console", func(ec zapcore.EncoderConfig) (zapcore.Encoder, error) {
return NewEncoder(ec), nil
})
func NewConfig() zap.Config {
return zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.DebugLevel),
Development: true,
Encoding: "pretty_console",
EncoderConfig: NewEncoderConfig(),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}
}
func NewEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder {
return &recordingEncoder{e: prettyConsoleEncoder{
buf: nil,
cfg: &cfg,
level: 0,
namespaceIndent: 0,
inList: false,
_listSepComma: "," + cfg.ConsoleSeparator,
_listSepSpace: cfg.ConsoleSeparator,
listSep: cfg.ConsoleSeparator,
}}
}
func NewEncoderConfig() zapcore.EncoderConfig {
return zapcore.EncoderConfig{
MessageKey: "M",
LevelKey: "L",
TimeKey: "T",
NameKey: "N",
CallerKey: zapcore.OmitKey,
FunctionKey: zapcore.OmitKey,
StacktraceKey: "S",
SkipLineEnding: false,
LineEnding: zapcore.DefaultLineEnding,
EncodeLevel: defaultLevelEncoder,
EncodeTime: DefaultTimeEncoder(time.Kitchen),
EncodeDuration: defaultDurationEncoder,
EncodeCaller: defaultCallerEncoder,
EncodeName: defaultNameEncoder,
NewReflectedEncoder: defaultReflectedEncoder,
ConsoleSeparator: " ",
}
}
func NewLogger(lvl zapcore.Level) *zap.Logger {
ec := NewEncoderConfig()
enc := NewEncoder(ec)
return zap.New(zapcore.NewCore(
enc,
os.Stdout,
lvl,
))
}
// Test interface conformance
var _ zapcore.Encoder = (*prettyConsoleEncoder)(nil)
type prettyConsoleEncoder struct {
buf *buffer.Buffer
cfg *zapcore.EncoderConfig
level zapcore.Level
namespaceIndent int
inList bool
listSep string
keyPrefix string
_listSepComma string
_listSepSpace string
}
// Clone implements zapcore.Encoder
func (e prettyConsoleEncoder) Clone() zapcore.Encoder {
clone := e.clone()
_, _ = clone.buf.Write(e.buf.Bytes())
return clone
}
func (e prettyConsoleEncoder) clone() *prettyConsoleEncoder {
clone := getPrettyConsoleEncoder()
clone.buf = getBuffer()
clone.cfg = e.cfg
clone.level = e.level
clone.namespaceIndent = e.namespaceIndent
clone.inList = e.inList
clone.listSep = e.listSep
clone.keyPrefix = e.keyPrefix
clone._listSepComma = e._listSepComma
clone._listSepSpace = e._listSepSpace
return clone
}
func (e prettyConsoleEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
e.buf = getBuffer()
e.level = entry.Level
raw := rawStringAppender{&e}
// Add preamble
if e.cfg.TimeKey != "" && e.cfg.EncodeTime != nil {
e.cfg.EncodeTime(entry.Time, raw)
}
if e.cfg.LevelKey != "" && e.cfg.EncodeLevel != nil {
e.cfg.EncodeLevel(entry.Level, raw)
}
if entry.LoggerName != "" && e.cfg.NameKey != "" && e.cfg.EncodeName != nil {
e.cfg.EncodeName(entry.LoggerName, raw)
}
if entry.Caller.Defined {
if e.cfg.CallerKey != "" && e.cfg.EncodeCaller != nil {
e.cfg.EncodeCaller(entry.Caller, raw)
}
if e.cfg.FunctionKey != "" {
raw.AppendString(entry.Caller.Function)
}
}
e.addSeparator()
// Kinda going about making it bold the long way here I imagine
e.buf.AppendString("\x1b[")
e.buf.AppendString(strconv.Itoa(colorBold))
e.buf.AppendString("m")
e.colorizeAtLevel(">")
e.buf.AppendString("\x1b[0m")
e.inList = true
// Add the message itself.
if entry.Message != "" && e.cfg.MessageKey != "" {
e.addSeparator()
e.appendSafeByte([]byte(entry.Message))
e.inList = true
}
// We are sorting all field keys alphabetically, except pushing multi-line
// stuff (array, reflect, object, error in that order) to the back.
//
// Additionally we are only sorting within namespace boundaries, as we don't
// want to re-order namespaces and destroy that structural information.
prev := 0
sortFunc := func(ii, jj int) bool {
ii += prev
jj += prev
if fields[ii].Type == fields[jj].Type {
return fields[ii].Key < fields[jj].Key
}
switch fields[ii].Type {
case zapcore.ArrayMarshalerType:
return fields[jj].Type == zapcore.ReflectType || fields[jj].Type == zapcore.ObjectMarshalerType || fields[jj].Type == zapcore.ErrorType
case zapcore.ReflectType:
return fields[jj].Type == zapcore.ObjectMarshalerType || fields[jj].Type == zapcore.ErrorType
case zapcore.ObjectMarshalerType:
return fields[jj].Type == zapcore.ErrorType
case zapcore.ErrorType:
return false
}
switch fields[jj].Type {
case zapcore.ArrayMarshalerType, zapcore.ReflectType, zapcore.ObjectMarshalerType, zapcore.ErrorType:
return true
default:
return fields[ii].Key < fields[jj].Key
}
}
for idx, field := range fields {
if field.Type == zapcore.NamespaceType {
sort.Slice(fields[prev:idx], sortFunc)
prev = idx + 1
} else if idx == len(fields)-1 {
sort.Slice(fields[prev:idx+1], sortFunc)
}
}
// Write the fields
for _, f := range fields {
if f.Type == zapcore.ErrorType {
if err := e.encodeError(f.Key, f.Interface.(error)); err != nil {
_ = e.encodeError(f.Key+"_PANIC_DISPLAYING_ERROR", err)
}
e.inList = false
} else {
f.AddTo(&e)
}
}
// Write the stacktrace
if entry.Stack != "" && e.cfg.StacktraceKey != "" {
e.namespaceIndent = 0
e.OpenNamespace("")
e.namespaceIndent += len("stacktrace=")
e.keyPrefix = ""
e.addIndentedString("stacktrace", strings.TrimPrefix(entry.Stack, "\n"))
}
// We're done :)
e.buf.AppendString(e.cfg.LineEnding)
return e.buf, nil
}
func (e *prettyConsoleEncoder) addSeparator() {
if e.inList {
e.colorizeAtLevel(e.listSep)
return
}
}
func (e *prettyConsoleEncoder) addKey(key string) {
e.colorizeAtLevel(e.keyPrefix + key + "=")
}
// addSafeString JSON-escapes a string and appends it to the internal buffer.
func (e *prettyConsoleEncoder) addSafeString(s string) {
for i := 0; i < len(s); {
if e.tryAddRune(s[i]) {
i++
continue
}
r, size := utf8.DecodeRuneInString(s[i:])
if e.tryAddRuneError(r, size) {
i++
continue
}
e.buf.AppendString(s[i : i+size])
i += size
}
}
// appendSafeByte is no-alloc equivalent of addSafeString(string(s)) for s
// []byte.
func (e *prettyConsoleEncoder) appendSafeByte(s []byte) {
for i := 0; i < len(s); {
if e.tryAddRune(s[i]) {
i++
continue
}
r, size := utf8.DecodeRune(s[i:])
if e.tryAddRuneError(r, size) {
i++
continue
}
_, _ = e.buf.Write(s[i : i+size]) // Explicitly ignore errors
i += size
}
}
// tryAddRune appends b if it is valid UTF-8 character represented in a
// single byte.
func (e *prettyConsoleEncoder) tryAddRune(b byte) bool {
const _hex = "0123456789abcdef"
if b >= utf8.RuneSelf {
return false
}
if 0x20 <= b && b != '\\' && b != '"' {
e.buf.AppendByte(b)
return true
}
switch b {
case '\\', '"':
e.colorizeAtLevel("\\" + string(b))
case '\n':
e.colorizeAtLevel("\\n")
case '\r':
e.colorizeAtLevel("\\r")
case '\t':
e.colorizeAtLevel("\\t")
default:
// Encode bytes < 0x20, except for the escape sequences above.
e.colorizeAtLevel(`\u00`)
e.colorizeAtLevel(string(_hex[b>>4]))
e.colorizeAtLevel(string(_hex[b&0xF]))
}
return true
}
func (e *prettyConsoleEncoder) tryAddRuneError(r rune, size int) bool {
if r == utf8.RuneError && size == 1 {
e.buf.AppendString(`\ufffd`)
return true
}
return false
}
// colorize returns the string s wrapped in ANSI code c, coloured properly for
// the logging level we're at.
func (e *prettyConsoleEncoder) colorizeAtLevel(s string) {
colorize(e.buf, s, defaultColours[e.level+defaultColourOffset]...)
}
// rawStringAppender will append strings without escaping them,
type rawStringAppender struct{ *prettyConsoleEncoder }
func (e rawStringAppender) AppendString(s string) {
e.addSeparator()
e.buf.AppendString(s)
e.inList = true
}
type indentingWriter struct {
buf io.Writer
indent int
lineEnding []byte
}
func (i indentingWriter) Write(p []byte) (n int, err error) {
if len(p) == 0 {
return 0, nil
}
idx := bytes.IndexByte(p, '\n')
if idx == -1 {
return i.buf.Write(p)
}
written, _ := i.buf.Write(p[0:idx])
read := written
n, _ = i.buf.Write(i.lineEnding)
written += n
read += 1
for read <= len(p) {
for ii := 0; ii < i.indent; ii++ {
n, _ := i.buf.Write([]byte(" "))
written += n
}
if read == len(p) {
return written, nil
}
idx = bytes.IndexByte(p[read:], '\n')
if idx == -1 {
n, _ := i.buf.Write(p[read:])
return written + n, nil
}
n, _ = i.buf.Write(p[read : read+idx])
written += n
read += n
n, _ = i.buf.Write(i.lineEnding)
written += n
read += 1
}
return written, nil
}