forked from rs/zerolog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.go
500 lines (457 loc) · 12.9 KB
/
field.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
package zerolog
import (
"encoding/json"
"fmt"
"sort"
"strconv"
"time"
)
func appendFields(dst []byte, fields map[string]interface{}) []byte {
keys := make([]string, 0, len(fields))
for key, _ := range fields {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
switch val := fields[key].(type) {
case string:
dst = appendString(dst, key, val)
case []byte:
dst = appendBytes(dst, key, val)
case error:
dst = appendErrorKey(dst, key, val)
case []error:
dst = appendErrorsKey(dst, key, val)
case bool:
dst = appendBool(dst, key, val)
case int:
dst = appendInt(dst, key, val)
case int8:
dst = appendInt8(dst, key, val)
case int16:
dst = appendInt16(dst, key, val)
case int32:
dst = appendInt32(dst, key, val)
case int64:
dst = appendInt64(dst, key, val)
case uint:
dst = appendUint(dst, key, val)
case uint8:
dst = appendUint8(dst, key, val)
case uint16:
dst = appendUint16(dst, key, val)
case uint32:
dst = appendUint32(dst, key, val)
case uint64:
dst = appendUint64(dst, key, val)
case float32:
dst = appendFloat32(dst, key, val)
case float64:
dst = appendFloat64(dst, key, val)
case time.Time:
dst = appendTime(dst, key, val)
case time.Duration:
dst = appendDuration(dst, key, val)
case []string:
dst = appendStrings(dst, key, val)
case []bool:
dst = appendBools(dst, key, val)
case []int:
dst = appendInts(dst, key, val)
case []int8:
dst = appendInts8(dst, key, val)
case []int16:
dst = appendInts16(dst, key, val)
case []int32:
dst = appendInts32(dst, key, val)
case []int64:
dst = appendInts64(dst, key, val)
case []uint:
dst = appendUints(dst, key, val)
// case []uint8:
// dst = appendUints8(dst, key, val)
case []uint16:
dst = appendUints16(dst, key, val)
case []uint32:
dst = appendUints32(dst, key, val)
case []uint64:
dst = appendUints64(dst, key, val)
case []float32:
dst = appendFloats32(dst, key, val)
case []float64:
dst = appendFloats64(dst, key, val)
case []time.Time:
dst = appendTimes(dst, key, val)
case []time.Duration:
dst = appendDurations(dst, key, val)
case nil:
dst = append(appendKey(dst, key), "null"...)
default:
dst = appendInterface(dst, key, val)
}
}
return dst
}
func appendKey(dst []byte, key string) []byte {
if len(dst) > 1 {
dst = append(dst, ',')
}
dst = appendJSONString(dst, key)
return append(dst, ':')
}
func appendString(dst []byte, key, val string) []byte {
return appendJSONString(appendKey(dst, key), val)
}
func appendStrings(dst []byte, key string, vals []string) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = appendJSONString(dst, vals[0])
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = appendJSONString(append(dst, ','), val)
}
}
dst = append(dst, ']')
return dst
}
func appendBytes(dst []byte, key string, val []byte) []byte {
return appendJSONBytes(appendKey(dst, key), val)
}
func appendErrorKey(dst []byte, key string, err error) []byte {
if err == nil {
return dst
}
return appendJSONString(appendKey(dst, key), err.Error())
}
func appendErrorsKey(dst []byte, key string, errs []error) []byte {
if len(errs) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
if errs[0] != nil {
dst = appendJSONString(dst, errs[0].Error())
} else {
dst = append(dst, "null"...)
}
if len(errs) > 1 {
for _, err := range errs[1:] {
if err == nil {
dst = append(dst, ",null"...)
continue
}
dst = appendJSONString(append(dst, ','), err.Error())
}
}
dst = append(dst, ']')
return dst
}
func appendError(dst []byte, err error) []byte {
return appendErrorKey(dst, ErrorFieldName, err)
}
func appendBool(dst []byte, key string, val bool) []byte {
return strconv.AppendBool(appendKey(dst, key), val)
}
func appendBools(dst []byte, key string, vals []bool) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendBool(dst, vals[0])
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendBool(append(dst, ','), val)
}
}
dst = append(dst, ']')
return dst
}
func appendInt(dst []byte, key string, val int) []byte {
return strconv.AppendInt(appendKey(dst, key), int64(val), 10)
}
func appendInts(dst []byte, key string, vals []int) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendInt(dst, int64(vals[0]), 10)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendInt(append(dst, ','), int64(val), 10)
}
}
dst = append(dst, ']')
return dst
}
func appendInt8(dst []byte, key string, val int8) []byte {
return strconv.AppendInt(appendKey(dst, key), int64(val), 10)
}
func appendInts8(dst []byte, key string, vals []int8) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendInt(dst, int64(vals[0]), 10)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendInt(append(dst, ','), int64(val), 10)
}
}
dst = append(dst, ']')
return dst
}
func appendInt16(dst []byte, key string, val int16) []byte {
return strconv.AppendInt(appendKey(dst, key), int64(val), 10)
}
func appendInts16(dst []byte, key string, vals []int16) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendInt(dst, int64(vals[0]), 10)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendInt(append(dst, ','), int64(val), 10)
}
}
dst = append(dst, ']')
return dst
}
func appendInt32(dst []byte, key string, val int32) []byte {
return strconv.AppendInt(appendKey(dst, key), int64(val), 10)
}
func appendInts32(dst []byte, key string, vals []int32) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendInt(dst, int64(vals[0]), 10)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendInt(append(dst, ','), int64(val), 10)
}
}
dst = append(dst, ']')
return dst
}
func appendInt64(dst []byte, key string, val int64) []byte {
return strconv.AppendInt(appendKey(dst, key), val, 10)
}
func appendInts64(dst []byte, key string, vals []int64) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendInt(dst, vals[0], 10)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendInt(append(dst, ','), val, 10)
}
}
dst = append(dst, ']')
return dst
}
func appendUint(dst []byte, key string, val uint) []byte {
return strconv.AppendUint(appendKey(dst, key), uint64(val), 10)
}
func appendUints(dst []byte, key string, vals []uint) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendUint(dst, uint64(vals[0]), 10)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendUint(append(dst, ','), uint64(val), 10)
}
}
dst = append(dst, ']')
return dst
}
func appendUint8(dst []byte, key string, val uint8) []byte {
return strconv.AppendUint(appendKey(dst, key), uint64(val), 10)
}
func appendUints8(dst []byte, key string, vals []uint8) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendUint(dst, uint64(vals[0]), 10)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendUint(append(dst, ','), uint64(val), 10)
}
}
dst = append(dst, ']')
return dst
}
func appendUint16(dst []byte, key string, val uint16) []byte {
return strconv.AppendUint(appendKey(dst, key), uint64(val), 10)
}
func appendUints16(dst []byte, key string, vals []uint16) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendUint(dst, uint64(vals[0]), 10)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendUint(append(dst, ','), uint64(val), 10)
}
}
dst = append(dst, ']')
return dst
}
func appendUint32(dst []byte, key string, val uint32) []byte {
return strconv.AppendUint(appendKey(dst, key), uint64(val), 10)
}
func appendUints32(dst []byte, key string, vals []uint32) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendUint(dst, uint64(vals[0]), 10)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendUint(append(dst, ','), uint64(val), 10)
}
}
dst = append(dst, ']')
return dst
}
func appendUint64(dst []byte, key string, val uint64) []byte {
return strconv.AppendUint(appendKey(dst, key), uint64(val), 10)
}
func appendUints64(dst []byte, key string, vals []uint64) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendUint(dst, vals[0], 10)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendUint(append(dst, ','), val, 10)
}
}
dst = append(dst, ']')
return dst
}
func appendFloat32(dst []byte, key string, val float32) []byte {
return strconv.AppendFloat(appendKey(dst, key), float64(val), 'f', -1, 32)
}
func appendFloats32(dst []byte, key string, vals []float32) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendFloat(dst, float64(vals[0]), 'f', -1, 32)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendFloat(append(dst, ','), float64(val), 'f', -1, 32)
}
}
dst = append(dst, ']')
return dst
}
func appendFloat64(dst []byte, key string, val float64) []byte {
return strconv.AppendFloat(appendKey(dst, key), val, 'f', -1, 32)
}
func appendFloats64(dst []byte, key string, vals []float64) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendFloat(dst, vals[0], 'f', -1, 32)
if len(vals) > 1 {
for _, val := range vals[1:] {
dst = strconv.AppendFloat(append(dst, ','), val, 'f', -1, 32)
}
}
dst = append(dst, ']')
return dst
}
func appendTime(dst []byte, key string, t time.Time) []byte {
if TimeFieldFormat == "" {
return appendInt64(dst, key, t.Unix())
}
return append(t.AppendFormat(append(appendKey(dst, key), '"'), TimeFieldFormat), '"')
}
func appendTimes(dst []byte, key string, vals []time.Time) []byte {
if TimeFieldFormat == "" {
return appendUnixTimes(dst, key, vals)
}
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = append(vals[0].AppendFormat(append(dst, '"'), TimeFieldFormat), '"')
if len(vals) > 1 {
for _, t := range vals[1:] {
dst = append(t.AppendFormat(append(dst, ',', '"'), TimeFieldFormat), '"')
}
}
dst = append(dst, ']')
return dst
}
func appendUnixTimes(dst []byte, key string, vals []time.Time) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendInt(dst, vals[0].Unix(), 10)
if len(vals) > 1 {
for _, t := range vals[1:] {
dst = strconv.AppendInt(dst, t.Unix(), 10)
}
}
dst = append(dst, ']')
return dst
}
func appendTimestamp(dst []byte) []byte {
return appendTime(dst, TimestampFieldName, TimestampFunc())
}
func appendDuration(dst []byte, key string, d time.Duration) []byte {
if DurationFieldInteger {
return appendInt64(dst, key, int64(d/DurationFieldUnit))
}
return appendFloat64(dst, key, float64(d)/float64(DurationFieldUnit))
}
func appendDurations(dst []byte, key string, vals []time.Duration) []byte {
if DurationFieldInteger {
return appendIntDurations(dst, key, vals)
}
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendFloat(dst, float64(vals[0])/float64(DurationFieldUnit), 'f', -1, 32)
if len(vals) > 1 {
for _, d := range vals[1:] {
dst = strconv.AppendFloat(append(dst, ','), float64(d)/float64(DurationFieldUnit), 'f', -1, 32)
}
}
dst = append(dst, ']')
return dst
}
func appendIntDurations(dst []byte, key string, vals []time.Duration) []byte {
if len(vals) == 0 {
return append(appendKey(dst, key), '[', ']')
}
dst = append(appendKey(dst, key), '[')
dst = strconv.AppendInt(dst, int64(vals[0]/DurationFieldUnit), 10)
if len(vals) > 1 {
for _, d := range vals[1:] {
dst = strconv.AppendInt(append(dst, ','), int64(d/DurationFieldUnit), 10)
}
}
dst = append(dst, ']')
return dst
}
func appendInterface(dst []byte, key string, i interface{}) []byte {
marshaled, err := json.Marshal(i)
if err != nil {
return appendString(dst, key, fmt.Sprintf("marshaling error: %v", err))
}
return append(appendKey(dst, key), marshaled...)
}