/
colorEncoder.go
366 lines (296 loc) · 7.82 KB
/
colorEncoder.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
package console
import (
"encoding/base64"
"fmt"
"strconv"
"sync"
"time"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
var poolColoredEncoder = sync.Pool{
New: func() interface{} {
return &coloredEncoder{
buf: bufferpool.Get(),
}
},
}
func getColoredEncoder(lvlColor uint16, scheme ColorScheme, disableColor bool) *coloredEncoder {
enc := poolColoredEncoder.Get().(*coloredEncoder)
enc.buf = bufferpool.Get()
enc.scheme = scheme
enc.entLevelColor = lvlColor
enc.disableColor = disableColor
return enc
}
func getLevelColor(level zapcore.Level, scheme ColorScheme) uint16 {
switch level {
case zapcore.DebugLevel:
return scheme.DebugLevel
case zapcore.InfoLevel:
return scheme.InfoLevel
case zapcore.ErrorLevel:
return scheme.ErrorLevel
case zapcore.PanicLevel:
return scheme.PanicLevel
case zapcore.WarnLevel:
return scheme.WarnLevel
case zapcore.DPanicLevel:
return scheme.DPanicLevel
case zapcore.FatalLevel:
return scheme.FatalLevel
default:
return scheme.String
}
}
func putColoredEncoder(enc *coloredEncoder) {
enc.scheme = defaultScheme
enc.buf.Free()
enc.entLevelColor = NoColor
enc.disableColor = false
enc.EncodeDuration = nil
enc.EncodeTime = nil
poolColoredEncoder.Put(enc)
}
type coloredEncoder struct {
buf *buffer.Buffer
disableColor bool
scheme ColorScheme
entLevelColor uint16
EncodeDuration zapcore.DurationEncoder
EncodeTime zapcore.TimeEncoder
}
func (e *coloredEncoder) addKey(key string) {
e.buf.AppendByte(' ')
e.appendColoredString(key, e.scheme.FieldName)
e.buf.AppendByte('=')
}
func (e *coloredEncoder) appendColoredString(val string, color uint16) {
if e.disableColor {
e.buf.AppendString(val)
return
}
colorizeTextW(e.buf, val, color)
}
func (e *coloredEncoder) AppendBool(val bool) {
e.addElementSeparator()
e.appendColoredString(strconv.FormatBool(val), e.scheme.Bool)
}
func (e *coloredEncoder) AppendByteString(bstr []byte) {
e.addElementSeparator()
e.buf.AppendString(string(bstr))
}
func (e *coloredEncoder) AppendComplex128(val complex128) {
r, i := float64(real(val)), float64(imag(val))
str := fmt.Sprintf("%s%s",
strconv.FormatFloat(r, 'f', -1, 64),
strconv.FormatFloat(i, 'f', -1, 64))
e.addElementSeparator()
e.appendColoredString(str, e.scheme.Float)
}
func (e *coloredEncoder) AppendComplex64(val complex64) {
e.AppendComplex128(complex128(val))
}
func (e *coloredEncoder) AppendFloat64(val float64) {
e.addElementSeparator()
e.appendColoredString(strconv.FormatFloat(val, 'f', -1, 64), e.scheme.Float)
}
func (e *coloredEncoder) AppendFloat32(val float32) {
e.AppendFloat64(float64(val))
}
func (e *coloredEncoder) AppendInt(val int) {
e.AppendInt64(int64(val))
}
func (e *coloredEncoder) AppendInt64(val int64) {
e.addElementSeparator()
e.appendColoredString(strconv.FormatInt(val, 10), e.scheme.Integer)
}
func (e *coloredEncoder) AppendInt32(val int32) {
e.AppendInt64(int64(val))
}
func (e *coloredEncoder) AppendInt16(val int16) {
e.AppendInt64(int64(val))
}
func (e *coloredEncoder) AppendInt8(val int8) {
e.AppendInt64(int64(val))
}
func (e *coloredEncoder) AppendString(str string) {
e.addElementSeparator()
if needsQuote(str) {
e.appendColoredString(strconv.Quote(str), e.scheme.String)
return
}
e.appendColoredString(str, e.scheme.String)
}
func needsQuote(s string) bool {
for i := range s {
if s[i] < 0x20 || s[i] > 0x7e || s[i] == ' ' || s[i] == '\\' || s[i] == '"' {
return true
}
}
return false
}
func (e *coloredEncoder) AppendUint(val uint) {
e.AppendUint64(uint64(val))
}
func (e *coloredEncoder) AppendUint64(val uint64) {
e.addElementSeparator()
e.appendColoredString(strconv.FormatUint(val, 64), e.scheme.PointerAddress)
}
func (e *coloredEncoder) AppendUint32(val uint32) {
e.AppendUint64(uint64(val))
}
func (e *coloredEncoder) AppendUint16(val uint16) {
e.AppendUint64(uint64(val))
}
func (e *coloredEncoder) AppendUint8(val uint8) {
e.AppendUint64(uint64(val))
}
func (e *coloredEncoder) AppendUintptr(val uintptr) {
e.AppendUint64(uint64(val))
}
func (e *coloredEncoder) AddArray(key string, marshaler zapcore.ArrayMarshaler) error {
e.addKey(key)
return e.AppendArray(marshaler)
}
func (e *coloredEncoder) AddObject(key string, obj zapcore.ObjectMarshaler) error {
e.addKey(key)
return e.AppendObject(obj)
}
func (e *coloredEncoder) AddBinary(key string, val []byte) {
e.AddString(key, base64.StdEncoding.EncodeToString(val))
}
func (e *coloredEncoder) AddByteString(key string, val []byte) {
e.AddString(key, base64.StdEncoding.EncodeToString(val))
}
func (e *coloredEncoder) AddBool(key string, val bool) {
e.addKey(key)
e.AppendBool(val)
}
func (e *coloredEncoder) AddComplex128(key string, val complex128) {
e.addKey(key)
e.AppendComplex128(val)
}
func (e *coloredEncoder) AddComplex64(key string, val complex64) {
e.addKey(key)
e.AppendComplex128(complex128(val))
}
func (e *coloredEncoder) AddDuration(key string, val time.Duration) {
e.addKey(key)
e.AppendDuration(val)
}
func (e *coloredEncoder) AddFloat64(key string, val float64) {
e.addKey(key)
e.AppendFloat64(val)
}
func (e *coloredEncoder) AddFloat32(key string, val float32) {
e.AddFloat64(key, float64(val))
}
func (e *coloredEncoder) AddInt(key string, val int) {
e.AddInt64(key, int64(val))
}
func (e *coloredEncoder) AddInt64(key string, val int64) {
e.addKey(key)
e.AppendInt64(val)
}
func (e *coloredEncoder) AddInt32(key string, val int32) {
e.AddInt64(key, int64(val))
}
func (e *coloredEncoder) AddInt16(key string, val int16) {
e.AddInt64(key, int64(val))
}
func (e *coloredEncoder) AddInt8(key string, val int8) {
e.AddInt64(key, int64(val))
}
func (e *coloredEncoder) AddString(key string, val string) {
e.addKey(key)
e.AppendString(val)
}
func (e *coloredEncoder) AddTime(key string, val time.Time) {
e.addKey(key)
e.AppendTime(val)
}
func (e *coloredEncoder) AddUint(key string, val uint) {
e.AddUint64(key, uint64(val))
}
func (e *coloredEncoder) AddUint64(key string, val uint64) {
e.addKey(key)
e.AppendUint64(val)
}
func (e *coloredEncoder) AddUint32(key string, val uint32) {
e.AddUint64(key, uint64(val))
}
func (e *coloredEncoder) AddUint16(key string, val uint16) {
e.AddUint64(key, uint64(val))
}
func (e *coloredEncoder) AddUint8(key string, val uint8) {
e.AddUint64(key, uint64(val))
}
func (e *coloredEncoder) AddUintptr(key string, val uintptr) {
e.AddUint64(key, uint64(val))
}
func (e *coloredEncoder) AddReflected(key string, val interface{}) error {
e.addKey(key)
v, ok := val.(string)
if !ok {
v = fmt.Sprintf("%v", val)
}
e.AppendString(v)
return nil
}
func (e *coloredEncoder) OpenNamespace(_ string) {
// no-op -
// namespaces do not really visually apply to console logs
}
func (e *coloredEncoder) AppendDuration(val time.Duration) {
// cur := e.buf.Len()
// e.EncodeDuration(val, e)
// if cur == e.buf.Len() {
// e.AppendInt64(int64(val))
// }
e.appendColoredString(val.String(), e.scheme.Time)
}
func (e *coloredEncoder) AppendTime(val time.Time) {
cur := e.buf.Len()
if e.EncodeTime != nil {
e.EncodeTime(val, e)
}
if cur == e.buf.Len() {
e.AppendInt64(val.UnixNano())
}
}
func (e *coloredEncoder) AppendArray(arr zapcore.ArrayMarshaler) error {
e.addElementSeparator()
e.buf.AppendByte('[')
err := arr.MarshalLogArray(e)
e.buf.AppendByte(']')
return err
}
func (e *coloredEncoder) AppendObject(obj zapcore.ObjectMarshaler) error {
e.addElementSeparator()
e.buf.AppendByte('{')
err := obj.MarshalLogObject(e)
e.buf.AppendByte('}')
return err
}
func (e *coloredEncoder) AppendReflected(val interface{}) error {
v, ok := val.(string)
if !ok {
v = fmt.Sprintf("%v", val)
}
e.addElementSeparator()
e.AppendString(v)
return nil
}
func (e *coloredEncoder) addElementSeparator() {
last := e.buf.Len() - 1
if last < 0 {
return
}
switch e.buf.Bytes()[last] {
case '{', '[', ':', ',', ' ', '=':
return
default:
e.buf.AppendByte(',')
}
}