-
Notifications
You must be signed in to change notification settings - Fork 0
/
zappretty.go
585 lines (501 loc) · 15.3 KB
/
zappretty.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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
// Inspiration came from a project known as zap-pretty: https://github.com/maoueh/zap-pretty
// Instead of a cli tool however, this is a native encoder implementing the zapcore.Encoder interface.
// A lot of this code came directly from the json encoder present in zap: https://github.com/uber-go/zap/blob/master/zapcore/json_encoder.go
package zappretty
import (
"encoding/base64"
"encoding/json"
"io"
"math"
"sync"
"time"
"unicode/utf8"
"github.com/fatih/color"
"go.uber.org/zap"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
const (
// For JSON-escaping; see jsonEncoder.safeAddString below.
hex = "0123456789abcdef"
timeFormat = "2006-01-02 15:04:05 MST"
)
var (
nullLiteralBytes = []byte("null")
cliPool = sync.Pool{New: func() interface{} {
return &cliEncoder{}
}}
bufPool = buffer.NewPool()
levelColor = map[zapcore.Level]color.Attribute{
zapcore.DebugLevel: color.FgBlue,
zapcore.InfoLevel: color.FgGreen,
zapcore.WarnLevel: color.FgYellow,
zapcore.ErrorLevel: color.FgRed,
zapcore.DPanicLevel: color.FgRed,
zapcore.PanicLevel: color.FgRed,
zapcore.FatalLevel: color.FgRed,
}
)
func Register(cfg zapcore.EncoderConfig) {
_ = zap.RegisterEncoder("cli", func(_ zapcore.EncoderConfig) (zapcore.Encoder, error) {
return NewCLIEncoder(cfg), nil
})
}
type cliEncoder struct {
*zapcore.EncoderConfig
buf *buffer.Buffer
openNamespaces int
// for encoding generic values by reflection
reflectBuf *buffer.Buffer
reflectEnc zapcore.ReflectedEncoder
}
func NewCLIEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder {
cfg.LineEnding = zapcore.DefaultLineEnding
if cfg.NewReflectedEncoder == nil {
cfg.NewReflectedEncoder = defaultReflectedEncoder
}
encoder := &cliEncoder{
EncoderConfig: &cfg,
buf: bufPool.Get(),
}
return encoder
}
func newCLIEncoder() *cliEncoder {
return &cliEncoder{}
}
func (enc *cliEncoder) Clone() zapcore.Encoder {
clone := enc.clone()
clone.buf.Write(enc.buf.Bytes())
return clone
}
func (enc *cliEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
final := enc.clone()
if final.TimeKey != "" {
final.encodeTimestamp(entry.Time)
}
if final.LevelKey != "" && final.EncodeLevel != nil {
final.encodeLevel(entry.Level)
}
if entry.LoggerName != "" && final.NameKey != "" {
final.encodeLoggerName(entry.LoggerName)
}
if entry.Caller.Defined && final.CallerKey != "" {
final.encodeCaller(entry.Caller)
}
if final.MessageKey != "" {
final.encodeMessage(entry.Message)
}
if enc.buf.Len() > 0 {
final.addElementSeparator()
final.buf.Write(enc.buf.Bytes())
}
if len(fields) > 0 {
final.buf.AppendString(colorize('{', color.FgWhite, color.Bold))
final.buf.AppendByte(' ')
}
// Add fields.
for i := range fields {
fields[i].AddTo(final)
}
final.closeOpenNamespaces()
if len(fields) > 0 {
final.buf.AppendByte(' ')
final.buf.AppendString(colorize('}', color.FgWhite, color.Bold))
}
final.buf.AppendString(final.LineEnding)
buf := final.buf
putCLIEncoder(final)
return buf, nil
}
func (enc *cliEncoder) AddArray(key string, marshaler zapcore.ArrayMarshaler) error {
enc.addKey(key)
return enc.AppendArray(marshaler)
}
func (enc *cliEncoder) AddObject(key string, marshaler zapcore.ObjectMarshaler) error {
enc.addKey(key)
return enc.AppendObject(marshaler)
}
func (enc *cliEncoder) AddBinary(key string, value []byte) {
enc.AddString(key, base64.StdEncoding.EncodeToString(value))
}
func (enc *cliEncoder) AddByteString(key string, value []byte) {
enc.addKey(key)
enc.AppendByteString(value)
}
func (enc *cliEncoder) AddBool(key string, value bool) {
enc.addKey(key)
enc.AppendBool(value)
}
func (enc *cliEncoder) AddComplex128(key string, value complex128) {
enc.addKey(key)
enc.appendComplex(value, 64)
}
func (enc *cliEncoder) AddComplex64(key string, value complex64) {
enc.addKey(key)
enc.appendComplex(complex128(value), 32)
}
func (enc *cliEncoder) AddDuration(key string, value time.Duration) {
enc.addKey(key)
enc.AppendDuration(value)
}
func (enc *cliEncoder) AddFloat64(key string, value float64) {
enc.addKey(key)
enc.AppendFloat64(value)
}
func (enc *cliEncoder) AddFloat32(key string, value float32) {
enc.addKey(key)
enc.AppendFloat32(value)
}
func (enc *cliEncoder) AddInt(key string, value int) {
enc.addKey(key)
enc.AddInt64(key, int64(value))
}
func (enc *cliEncoder) AddInt64(key string, value int64) {
enc.addKey(key)
enc.AppendInt64(value)
}
func (enc *cliEncoder) AddInt32(key string, value int32) {
enc.addKey(key)
enc.AddInt64(key, int64(value))
}
func (enc *cliEncoder) AddInt16(key string, value int16) {
enc.addKey(key)
enc.AddInt64(key, int64(value))
}
func (enc *cliEncoder) AddInt8(key string, value int8) {
enc.addKey(key)
enc.AddInt64(key, int64(value))
}
func (enc *cliEncoder) AddString(key, value string) {
enc.addKey(key)
enc.AppendString(value)
}
func (enc *cliEncoder) AddTime(key string, value time.Time) {
enc.addKey(key)
enc.AppendTime(value)
}
func (enc *cliEncoder) AddUint(key string, value uint) {
enc.addKey(key)
enc.AddUint64(key, uint64(value))
}
func (enc *cliEncoder) AddUint64(key string, value uint64) {
enc.addKey(key)
enc.AppendUint64(value)
}
func (enc *cliEncoder) AddUint32(key string, value uint32) {
enc.addKey(key)
enc.AddUint64(key, uint64(value))
}
func (enc *cliEncoder) AddUint16(key string, value uint16) {
enc.addKey(key)
enc.AddUint64(key, uint64(value))
}
func (enc *cliEncoder) AddUint8(key string, value uint8) {
enc.addKey(key)
enc.AddUint64(key, uint64(value))
}
func (enc *cliEncoder) AddUintptr(key string, value uintptr) {
enc.addKey(key)
enc.AddUint64(key, uint64(value))
}
// AddReflected uses reflection to serialize arbitrary objects, so it can be
// slow and allocation-heavy.
func (enc *cliEncoder) AddReflected(key string, value interface{}) error {
valueBytes, err := enc.encodeReflected(value)
if err != nil {
return err
}
enc.addKey(key)
_, err = enc.buf.Write(valueBytes)
return err
}
// OpenNamespace opens an isolated namespace where all subsequent fields will
// be added. Applications can use namespaces to prevent key collisions when
// injecting loggers into sub-components or third-party libraries.
func (enc *cliEncoder) OpenNamespace(key string) {
enc.addKey(key)
enc.buf.AppendByte('{')
enc.openNamespaces++
}
// The following implements the PrimitiveArrayEncoder and ArrayEncoder interfaces.
func (enc *cliEncoder) AppendBool(value bool) {
enc.addElementSeparator()
enc.buf.AppendBool(value)
}
func (enc *cliEncoder) AppendByteString(value []byte) {
enc.addElementSeparator()
enc.buf.AppendByte('"')
enc.safeAddByteString(value)
enc.buf.AppendByte('"')
}
func (enc *cliEncoder) AppendComplex128(value complex128) { enc.appendComplex(complex128(value), 64) }
func (enc *cliEncoder) AppendComplex64(value complex64) { enc.appendComplex(complex128(value), 32) }
func (enc *cliEncoder) AppendFloat64(value float64) { enc.appendFloat(value, 64) }
func (enc *cliEncoder) AppendFloat32(value float32) { enc.appendFloat(float64(value), 32) }
func (enc *cliEncoder) AppendInt(value int) { enc.AppendInt64(int64(value)) }
func (enc *cliEncoder) AppendInt64(value int64) {
enc.addElementSeparator()
enc.buf.AppendInt(value)
}
func (enc *cliEncoder) AppendInt32(value int32) { enc.AppendInt64(int64(value)) }
func (enc *cliEncoder) AppendInt16(value int16) { enc.AppendInt64(int64(value)) }
func (enc *cliEncoder) AppendInt8(value int8) { enc.AppendInt64(int64(value)) }
func (enc *cliEncoder) AppendString(value string) {
enc.addElementSeparator()
enc.buf.AppendString(colorize('"', color.FgGreen))
enc.buf.AppendString(colorize(value, color.FgGreen))
enc.buf.AppendString(colorize('"', color.FgGreen))
}
func (enc *cliEncoder) AppendUint(value uint) { enc.AppendUint64(uint64(value)) }
func (enc *cliEncoder) AppendUint64(value uint64) { enc.AppendUint64(uint64(value)) }
func (enc *cliEncoder) AppendUint32(value uint32) { enc.AppendUint64(uint64(value)) }
func (enc *cliEncoder) AppendUint16(value uint16) { enc.AppendUint64(uint64(value)) }
func (enc *cliEncoder) AppendUint8(value uint8) { enc.AppendUint64(uint64(value)) }
func (enc *cliEncoder) AppendUintptr(value uintptr) { enc.AppendUint64(uint64(value)) }
func (enc *cliEncoder) AppendDuration(value time.Duration) {
cur := enc.buf.Len()
if e := enc.EncodeDuration; e != nil {
e(value, enc)
}
if cur == enc.buf.Len() {
// User-supplied EncodeDuration is a no-op. Fall back to nanoseconds to keep
// JSON valid.
enc.AppendInt64(int64(value))
}
}
func (enc *cliEncoder) AppendTime(value time.Time) {
cur := enc.buf.Len()
if e := enc.EncodeTime; e != nil {
e(value, enc)
}
if cur == enc.buf.Len() {
// User-supplied EncodeTime is a no-op. Fall back to nanos since epoch to keep
// output JSON valueid.
enc.AppendInt64(value.UnixNano())
}
}
func (enc *cliEncoder) AppendArray(value zapcore.ArrayMarshaler) error {
enc.addElementSeparator()
enc.buf.AppendString(colorize('[', color.FgWhite, color.Bold))
err := value.MarshalLogArray(enc)
enc.buf.AppendString(colorize(']', color.FgWhite, color.Bold))
return err
}
func (enc *cliEncoder) AppendObject(value zapcore.ObjectMarshaler) error {
// Close ONLY new openNamespaces that are created during
// AppendObject().
old := enc.openNamespaces
enc.openNamespaces = 0
enc.addElementSeparator()
enc.buf.AppendString(colorize('{', color.FgWhite, color.Bold))
err := value.MarshalLogObject(enc)
enc.buf.AppendString(colorize('}', color.FgWhite, color.Bold))
enc.closeOpenNamespaces()
enc.openNamespaces = old
return err
}
func (enc *cliEncoder) AppendReflected(value interface{}) error {
valueBytes, err := enc.encodeReflected(value)
if err != nil {
return err
}
enc.addElementSeparator()
_, err = enc.buf.Write(valueBytes)
return err
}
// Only invoke the standard JSON encoder if there is actually something to
// encode; otherwise write JSON null literal directly.
func (enc *cliEncoder) encodeReflected(obj interface{}) ([]byte, error) {
if obj == nil {
return nullLiteralBytes, nil
}
enc.resetReflectBuf()
if err := enc.reflectEnc.Encode(obj); err != nil {
return nil, err
}
enc.reflectBuf.TrimNewline()
return enc.reflectBuf.Bytes(), nil
}
func (enc *cliEncoder) resetReflectBuf() {
if enc.reflectBuf == nil {
enc.reflectBuf = bufPool.Get()
enc.reflectEnc = enc.NewReflectedEncoder(enc.reflectBuf)
} else {
enc.reflectBuf.Reset()
}
}
// appendComplex appends the encoded form of the provided complex128 value.
// precision specifies the encoding precision for the real and imaginary
// components of the complex number.
func (enc *cliEncoder) appendComplex(val complex128, precision int) {
enc.addElementSeparator()
// Cast to a platform-independent, fixed-size type.
r, i := float64(real(val)), float64(imag(val))
enc.buf.AppendByte('"')
// Because we're always in a quoted string, we can use strconv without
// special-casing NaN and +/-Inf.
enc.buf.AppendFloat(r, precision)
// If imaginary part is less than 0, minus (-) sign is added by default
// by AppendFloat.
if i >= 0 {
enc.buf.AppendByte('+')
}
enc.buf.AppendFloat(i, precision)
enc.buf.AppendByte('i')
enc.buf.AppendByte('"')
}
func (enc *cliEncoder) appendFloat(val float64, bitSize int) {
enc.addElementSeparator()
switch {
case math.IsNaN(val):
enc.buf.AppendString(`"NaN"`)
case math.IsInf(val, 1):
enc.buf.AppendString(`"+Inf"`)
case math.IsInf(val, -1):
enc.buf.AppendString(`"-Inf"`)
default:
enc.buf.AppendFloat(val, bitSize)
}
}
func (enc *cliEncoder) clone() *cliEncoder {
clone := getCLIEncoder()
clone.EncoderConfig = enc.EncoderConfig
clone.buf = bufPool.Get()
return clone
}
func (enc *cliEncoder) encodeTimestamp(timestamp time.Time) {
enc.buf.WriteString(color.New(color.FgWhite).Sprintf("[%s]", timestamp.Format(timeFormat)))
enc.buf.WriteString(" ")
}
func (enc *cliEncoder) encodeLevel(level zapcore.Level) {
if level == zapcore.InfoLevel {
enc.buf.WriteString(color.New(levelColor[level]).Sprint(level.CapitalString() + " "))
} else {
enc.buf.WriteString(color.New(levelColor[level]).Sprint(level.CapitalString()))
}
enc.buf.WriteString(" ")
}
func (enc *cliEncoder) encodeLoggerName(logger string) {
enc.buf.WriteString(color.New(color.FgHiBlack).Sprint(logger))
enc.buf.WriteString(" ")
}
func (enc *cliEncoder) encodeCaller(caller zapcore.EntryCaller) {
enc.buf.WriteString(color.New(color.FgHiBlack).Sprintf("(%s)", caller.TrimmedPath()))
enc.buf.WriteString(" ")
}
func (enc *cliEncoder) encodeMessage(message string) {
enc.buf.WriteString(color.New(color.FgHiWhite).Sprint(message))
enc.buf.WriteString(" ")
}
func (enc *cliEncoder) addKey(key string) {
enc.addElementSeparator()
enc.buf.AppendString(colorize('"', color.FgBlue, color.Bold))
enc.buf.AppendString(colorize(key, color.FgBlue, color.Bold))
enc.buf.AppendString(colorize('"', color.FgBlue, color.Bold))
enc.buf.AppendString(colorize(':', color.FgBlue, color.Bold))
enc.buf.AppendByte(' ')
}
func (enc *cliEncoder) addElementSeparator() {
last := enc.buf.Len() - 1
if last < 0 {
return
}
switch enc.buf.Bytes()[last] {
case '{', '[', ':', ',', ' ':
return
default:
enc.buf.AppendString(colorize(',', color.FgWhite, color.Bold))
enc.buf.AppendByte(' ')
}
}
func (enc *cliEncoder) closeOpenNamespaces() {
for i := 0; i < enc.openNamespaces; i++ {
enc.buf.AppendString(colorize('}', color.FgWhite, color.Bold))
}
enc.openNamespaces = 0
}
// safeAddByteString is no-alloc equivalent of safeAddString(string(s)) for s
// []byte.
func (enc *cliEncoder) safeAddByteString(s []byte) {
for i := 0; i < len(s); {
if enc.tryAddRuneSelf(s[i]) {
i++
continue
}
r, size := utf8.DecodeRune(s[i:])
if enc.tryAddRuneError(r, size) {
i++
continue
}
enc.buf.Write(s[i : i+size])
i += size
}
}
// tryAddRuneSelf appends b if it is valid UTF-8 character represented in a
// single byte.
func (enc *cliEncoder) tryAddRuneSelf(b byte) bool {
if b >= utf8.RuneSelf {
return false
}
if 0x20 <= b && b != '\\' && b != '"' {
enc.buf.AppendByte(b)
return true
}
switch b {
case '\\', '"':
enc.buf.AppendByte('\\')
enc.buf.AppendByte(b)
case '\n':
enc.buf.AppendByte('\\')
enc.buf.AppendByte('n')
case '\r':
enc.buf.AppendByte('\\')
enc.buf.AppendByte('r')
case '\t':
enc.buf.AppendByte('\\')
enc.buf.AppendByte('t')
default:
// Encode bytes < 0x20, except for the escape sequences above.
enc.buf.AppendString(`\u00`)
enc.buf.AppendByte(hex[b>>4])
enc.buf.AppendByte(hex[b&0xF])
}
return true
}
func (enc *cliEncoder) tryAddRuneError(r rune, size int) bool {
if r == utf8.RuneError && size == 1 {
enc.buf.AppendString(`\ufffd`)
return true
}
return false
}
func getCLIEncoder() *cliEncoder {
return cliPool.Get().(*cliEncoder)
}
func putCLIEncoder(enc *cliEncoder) {
if enc.reflectBuf != nil {
enc.reflectBuf.Free()
}
enc.EncoderConfig = nil
enc.buf = nil
enc.openNamespaces = 0
enc.reflectBuf = nil
enc.reflectEnc = nil
cliPool.Put(enc)
}
func defaultReflectedEncoder(w io.Writer) zapcore.ReflectedEncoder {
enc := json.NewEncoder(w)
// For consistency with our custom JSON encoder.
enc.SetEscapeHTML(false)
return enc
}
func colorize(arg any, attributes ...color.Attribute) string {
switch x := arg.(type) {
case byte:
return color.New(attributes...).Sprint(string(x))
case rune:
return color.New(attributes...).Sprint(string(x))
default:
return color.New(attributes...).Sprint(arg)
}
}