1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package zap
22
23 import (
24 "fmt"
25 "math"
26 "time"
27
28 "go.uber.org/zap/internal/stacktrace"
29 "go.uber.org/zap/zapcore"
30 )
31
32
33
34 type Field = zapcore.Field
35
36 var (
37 _minTimeInt64 = time.Unix(0, math.MinInt64)
38 _maxTimeInt64 = time.Unix(0, math.MaxInt64)
39 )
40
41
42
43 func Skip() Field {
44 return Field{Type: zapcore.SkipType}
45 }
46
47
48
49
50
51 func nilField(key string) Field { return Reflect(key, nil) }
52
53
54
55
56
57
58 func Binary(key string, val []byte) Field {
59 return Field{Key: key, Type: zapcore.BinaryType, Interface: val}
60 }
61
62
63 func Bool(key string, val bool) Field {
64 var ival int64
65 if val {
66 ival = 1
67 }
68 return Field{Key: key, Type: zapcore.BoolType, Integer: ival}
69 }
70
71
72
73 func Boolp(key string, val *bool) Field {
74 if val == nil {
75 return nilField(key)
76 }
77 return Bool(key, *val)
78 }
79
80
81
82
83 func ByteString(key string, val []byte) Field {
84 return Field{Key: key, Type: zapcore.ByteStringType, Interface: val}
85 }
86
87
88
89
90 func Complex128(key string, val complex128) Field {
91 return Field{Key: key, Type: zapcore.Complex128Type, Interface: val}
92 }
93
94
95
96 func Complex128p(key string, val *complex128) Field {
97 if val == nil {
98 return nilField(key)
99 }
100 return Complex128(key, *val)
101 }
102
103
104
105
106 func Complex64(key string, val complex64) Field {
107 return Field{Key: key, Type: zapcore.Complex64Type, Interface: val}
108 }
109
110
111
112 func Complex64p(key string, val *complex64) Field {
113 if val == nil {
114 return nilField(key)
115 }
116 return Complex64(key, *val)
117 }
118
119
120
121
122 func Float64(key string, val float64) Field {
123 return Field{Key: key, Type: zapcore.Float64Type, Integer: int64(math.Float64bits(val))}
124 }
125
126
127
128 func Float64p(key string, val *float64) Field {
129 if val == nil {
130 return nilField(key)
131 }
132 return Float64(key, *val)
133 }
134
135
136
137
138 func Float32(key string, val float32) Field {
139 return Field{Key: key, Type: zapcore.Float32Type, Integer: int64(math.Float32bits(val))}
140 }
141
142
143
144 func Float32p(key string, val *float32) Field {
145 if val == nil {
146 return nilField(key)
147 }
148 return Float32(key, *val)
149 }
150
151
152 func Int(key string, val int) Field {
153 return Int64(key, int64(val))
154 }
155
156
157
158 func Intp(key string, val *int) Field {
159 if val == nil {
160 return nilField(key)
161 }
162 return Int(key, *val)
163 }
164
165
166 func Int64(key string, val int64) Field {
167 return Field{Key: key, Type: zapcore.Int64Type, Integer: val}
168 }
169
170
171
172 func Int64p(key string, val *int64) Field {
173 if val == nil {
174 return nilField(key)
175 }
176 return Int64(key, *val)
177 }
178
179
180 func Int32(key string, val int32) Field {
181 return Field{Key: key, Type: zapcore.Int32Type, Integer: int64(val)}
182 }
183
184
185
186 func Int32p(key string, val *int32) Field {
187 if val == nil {
188 return nilField(key)
189 }
190 return Int32(key, *val)
191 }
192
193
194 func Int16(key string, val int16) Field {
195 return Field{Key: key, Type: zapcore.Int16Type, Integer: int64(val)}
196 }
197
198
199
200 func Int16p(key string, val *int16) Field {
201 if val == nil {
202 return nilField(key)
203 }
204 return Int16(key, *val)
205 }
206
207
208 func Int8(key string, val int8) Field {
209 return Field{Key: key, Type: zapcore.Int8Type, Integer: int64(val)}
210 }
211
212
213
214 func Int8p(key string, val *int8) Field {
215 if val == nil {
216 return nilField(key)
217 }
218 return Int8(key, *val)
219 }
220
221
222 func String(key string, val string) Field {
223 return Field{Key: key, Type: zapcore.StringType, String: val}
224 }
225
226
227
228 func Stringp(key string, val *string) Field {
229 if val == nil {
230 return nilField(key)
231 }
232 return String(key, *val)
233 }
234
235
236 func Uint(key string, val uint) Field {
237 return Uint64(key, uint64(val))
238 }
239
240
241
242 func Uintp(key string, val *uint) Field {
243 if val == nil {
244 return nilField(key)
245 }
246 return Uint(key, *val)
247 }
248
249
250 func Uint64(key string, val uint64) Field {
251 return Field{Key: key, Type: zapcore.Uint64Type, Integer: int64(val)}
252 }
253
254
255
256 func Uint64p(key string, val *uint64) Field {
257 if val == nil {
258 return nilField(key)
259 }
260 return Uint64(key, *val)
261 }
262
263
264 func Uint32(key string, val uint32) Field {
265 return Field{Key: key, Type: zapcore.Uint32Type, Integer: int64(val)}
266 }
267
268
269
270 func Uint32p(key string, val *uint32) Field {
271 if val == nil {
272 return nilField(key)
273 }
274 return Uint32(key, *val)
275 }
276
277
278 func Uint16(key string, val uint16) Field {
279 return Field{Key: key, Type: zapcore.Uint16Type, Integer: int64(val)}
280 }
281
282
283
284 func Uint16p(key string, val *uint16) Field {
285 if val == nil {
286 return nilField(key)
287 }
288 return Uint16(key, *val)
289 }
290
291
292 func Uint8(key string, val uint8) Field {
293 return Field{Key: key, Type: zapcore.Uint8Type, Integer: int64(val)}
294 }
295
296
297
298 func Uint8p(key string, val *uint8) Field {
299 if val == nil {
300 return nilField(key)
301 }
302 return Uint8(key, *val)
303 }
304
305
306 func Uintptr(key string, val uintptr) Field {
307 return Field{Key: key, Type: zapcore.UintptrType, Integer: int64(val)}
308 }
309
310
311
312 func Uintptrp(key string, val *uintptr) Field {
313 if val == nil {
314 return nilField(key)
315 }
316 return Uintptr(key, *val)
317 }
318
319
320
321
322
323
324
325
326 func Reflect(key string, val interface{}) Field {
327 return Field{Key: key, Type: zapcore.ReflectType, Interface: val}
328 }
329
330
331
332
333
334
335 func Namespace(key string) Field {
336 return Field{Key: key, Type: zapcore.NamespaceType}
337 }
338
339
340
341 func Stringer(key string, val fmt.Stringer) Field {
342 return Field{Key: key, Type: zapcore.StringerType, Interface: val}
343 }
344
345
346
347 func Time(key string, val time.Time) Field {
348 if val.Before(_minTimeInt64) || val.After(_maxTimeInt64) {
349 return Field{Key: key, Type: zapcore.TimeFullType, Interface: val}
350 }
351 return Field{Key: key, Type: zapcore.TimeType, Integer: val.UnixNano(), Interface: val.Location()}
352 }
353
354
355
356 func Timep(key string, val *time.Time) Field {
357 if val == nil {
358 return nilField(key)
359 }
360 return Time(key, *val)
361 }
362
363
364
365
366
367 func Stack(key string) Field {
368 return StackSkip(key, 1)
369 }
370
371
372
373 func StackSkip(key string, skip int) Field {
374
375
376
377
378 return String(key, stacktrace.Take(skip+1))
379 }
380
381
382
383 func Duration(key string, val time.Duration) Field {
384 return Field{Key: key, Type: zapcore.DurationType, Integer: int64(val)}
385 }
386
387
388
389 func Durationp(key string, val *time.Duration) Field {
390 if val == nil {
391 return nilField(key)
392 }
393 return Duration(key, *val)
394 }
395
396
397
398
399
400 func Object(key string, val zapcore.ObjectMarshaler) Field {
401 return Field{Key: key, Type: zapcore.ObjectMarshalerType, Interface: val}
402 }
403
404
405
406
407 func Inline(val zapcore.ObjectMarshaler) Field {
408 return zapcore.Field{
409 Type: zapcore.InlineMarshalerType,
410 Interface: val,
411 }
412 }
413
414
415
416 func Dict(key string, val ...Field) Field {
417 return dictField(key, val)
418 }
419
420
421 func dictField(key string, val []Field) Field {
422 return Object(key, dictObject(val))
423 }
424
425 type dictObject []Field
426
427 func (d dictObject) MarshalLogObject(enc zapcore.ObjectEncoder) error {
428 for _, f := range d {
429 f.AddTo(enc)
430 }
431 return nil
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 type anyFieldC[T any] func(string, T) Field
466
467 func (f anyFieldC[T]) Any(key string, val any) Field {
468 v, _ := val.(T)
469
470 return f(key, v)
471 }
472
473
474
475
476
477
478
479
480 func Any(key string, value interface{}) Field {
481 var c interface{ Any(string, any) Field }
482
483 switch value.(type) {
484 case zapcore.ObjectMarshaler:
485 c = anyFieldC[zapcore.ObjectMarshaler](Object)
486 case zapcore.ArrayMarshaler:
487 c = anyFieldC[zapcore.ArrayMarshaler](Array)
488 case []Field:
489 c = anyFieldC[[]Field](dictField)
490 case bool:
491 c = anyFieldC[bool](Bool)
492 case *bool:
493 c = anyFieldC[*bool](Boolp)
494 case []bool:
495 c = anyFieldC[[]bool](Bools)
496 case complex128:
497 c = anyFieldC[complex128](Complex128)
498 case *complex128:
499 c = anyFieldC[*complex128](Complex128p)
500 case []complex128:
501 c = anyFieldC[[]complex128](Complex128s)
502 case complex64:
503 c = anyFieldC[complex64](Complex64)
504 case *complex64:
505 c = anyFieldC[*complex64](Complex64p)
506 case []complex64:
507 c = anyFieldC[[]complex64](Complex64s)
508 case float64:
509 c = anyFieldC[float64](Float64)
510 case *float64:
511 c = anyFieldC[*float64](Float64p)
512 case []float64:
513 c = anyFieldC[[]float64](Float64s)
514 case float32:
515 c = anyFieldC[float32](Float32)
516 case *float32:
517 c = anyFieldC[*float32](Float32p)
518 case []float32:
519 c = anyFieldC[[]float32](Float32s)
520 case int:
521 c = anyFieldC[int](Int)
522 case *int:
523 c = anyFieldC[*int](Intp)
524 case []int:
525 c = anyFieldC[[]int](Ints)
526 case int64:
527 c = anyFieldC[int64](Int64)
528 case *int64:
529 c = anyFieldC[*int64](Int64p)
530 case []int64:
531 c = anyFieldC[[]int64](Int64s)
532 case int32:
533 c = anyFieldC[int32](Int32)
534 case *int32:
535 c = anyFieldC[*int32](Int32p)
536 case []int32:
537 c = anyFieldC[[]int32](Int32s)
538 case int16:
539 c = anyFieldC[int16](Int16)
540 case *int16:
541 c = anyFieldC[*int16](Int16p)
542 case []int16:
543 c = anyFieldC[[]int16](Int16s)
544 case int8:
545 c = anyFieldC[int8](Int8)
546 case *int8:
547 c = anyFieldC[*int8](Int8p)
548 case []int8:
549 c = anyFieldC[[]int8](Int8s)
550 case string:
551 c = anyFieldC[string](String)
552 case *string:
553 c = anyFieldC[*string](Stringp)
554 case []string:
555 c = anyFieldC[[]string](Strings)
556 case uint:
557 c = anyFieldC[uint](Uint)
558 case *uint:
559 c = anyFieldC[*uint](Uintp)
560 case []uint:
561 c = anyFieldC[[]uint](Uints)
562 case uint64:
563 c = anyFieldC[uint64](Uint64)
564 case *uint64:
565 c = anyFieldC[*uint64](Uint64p)
566 case []uint64:
567 c = anyFieldC[[]uint64](Uint64s)
568 case uint32:
569 c = anyFieldC[uint32](Uint32)
570 case *uint32:
571 c = anyFieldC[*uint32](Uint32p)
572 case []uint32:
573 c = anyFieldC[[]uint32](Uint32s)
574 case uint16:
575 c = anyFieldC[uint16](Uint16)
576 case *uint16:
577 c = anyFieldC[*uint16](Uint16p)
578 case []uint16:
579 c = anyFieldC[[]uint16](Uint16s)
580 case uint8:
581 c = anyFieldC[uint8](Uint8)
582 case *uint8:
583 c = anyFieldC[*uint8](Uint8p)
584 case []byte:
585 c = anyFieldC[[]byte](Binary)
586 case uintptr:
587 c = anyFieldC[uintptr](Uintptr)
588 case *uintptr:
589 c = anyFieldC[*uintptr](Uintptrp)
590 case []uintptr:
591 c = anyFieldC[[]uintptr](Uintptrs)
592 case time.Time:
593 c = anyFieldC[time.Time](Time)
594 case *time.Time:
595 c = anyFieldC[*time.Time](Timep)
596 case []time.Time:
597 c = anyFieldC[[]time.Time](Times)
598 case time.Duration:
599 c = anyFieldC[time.Duration](Duration)
600 case *time.Duration:
601 c = anyFieldC[*time.Duration](Durationp)
602 case []time.Duration:
603 c = anyFieldC[[]time.Duration](Durations)
604 case error:
605 c = anyFieldC[error](NamedError)
606 case []error:
607 c = anyFieldC[[]error](Errors)
608 case fmt.Stringer:
609 c = anyFieldC[fmt.Stringer](Stringer)
610 default:
611 c = anyFieldC[any](Reflect)
612 }
613
614 return c.Any(key, value)
615 }
616
View as plain text