This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfield.go
989 lines (835 loc) · 21.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
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
package rec
import (
"encoding/json"
"fmt"
"math"
"strconv"
"time"
)
const (
errorKey = "error"
errorsKey = "errors"
errorStacktraceKey = "errorStacktrace"
)
// Type is enum for rec.Field.
type Type uint8
const (
typeNone Type = iota
typeBool
typeBoolPtr
typeUint
typeUintPtr
typeUint8
typeUint8Ptr
typeUint16
typeUint16Ptr
typeUint32
typeUint32Ptr
typeUint64
typeUint64Ptr
typeInt
typeIntPtr
typeInt8
typeInt8Ptr
typeInt16
typeInt16Ptr
typeInt32
typeInt32Ptr
typeInt64
typeInt64Ptr
typeFloat32
typeFloat32Ptr
typeFloat64
typeFloat64Ptr
typeComplex64
typeComplex64Ptr
typeComplex128
typeComplex128Ptr
typeTime
typeTimeFormat
typeTimePtr
typeTimeFormatPtr
typeDuration
typeDurationPtr
typeDurationFormat
typeDurationFormatPtr
typeString
typeStrings
typeStringPtr
typeStringer
typeFormatter
typeError
typeErrors
typeErrorStacktrace
typeInterface
typeObject
)
// DefaultTimeFormat is default time format for rec.Time() and rec.TimePtr().
const DefaultTimeFormat = time.RFC3339Nano
// CustomTimeFormat is time format for rec.Time() and rec.TimePtr().
var CustomTimeFormat = DefaultTimeFormat // nolint: gochecknoglobals
// Field is a struct for adding fields to the rec JSON log.
type Field struct {
t Type
key string
interfacevalue1 interface{}
boolvalue1 bool
int64value1 int64
int64value2 int64
uint64value1 uint64
float64value1 float64
float64value2 float64
stringvalue1 string
}
func appendJSONField(dst []byte, f Field) []byte {
dst = append(dst, '"')
dst = appendJSONEscapedString(dst, f.key)
dst = append(dst, '"')
dst = append(dst, ':')
dst = appendFieldValue(dst, f, json.Marshal)
return dst
}
// nolint: cyclop, funlen, gocognit, gocyclo, maintidx
func appendFieldValue(dst []byte, f Field, jsonMarshalFn func(interface{}) ([]byte, error)) []byte {
const (
b10 = 10
b32 = 32
b64 = 64
null = `null`
)
switch f.t {
// bool
case typeBool:
dst = strconv.AppendBool(dst, f.boolvalue1)
case typeBoolPtr:
value, ok := f.interfacevalue1.(*bool)
if ok && value != nil {
dst = strconv.AppendBool(dst, *value)
break
}
dst = append(dst, null...)
// number
case typeUint:
dst = strconv.AppendUint(dst, f.uint64value1, b10)
case typeUintPtr:
value, ok := f.interfacevalue1.(*uint)
if ok && value != nil {
dst = strconv.AppendUint(dst, uint64(*value), b10)
break
}
dst = append(dst, null...)
case typeUint8:
dst = strconv.AppendUint(dst, f.uint64value1, b10)
case typeUint8Ptr:
value, ok := f.interfacevalue1.(*uint8)
if ok && value != nil {
dst = strconv.AppendUint(dst, uint64(*value), b10)
break
}
dst = append(dst, null...)
case typeUint16:
dst = strconv.AppendUint(dst, f.uint64value1, b10)
case typeUint16Ptr:
value, ok := f.interfacevalue1.(*uint16)
if ok && value != nil {
dst = strconv.AppendUint(dst, uint64(*value), b10)
break
}
dst = append(dst, null...)
case typeUint32:
dst = strconv.AppendUint(dst, f.uint64value1, b10)
case typeUint32Ptr:
value, ok := f.interfacevalue1.(*uint32)
if ok && value != nil {
dst = strconv.AppendUint(dst, uint64(*value), b10)
break
}
dst = append(dst, null...)
case typeUint64:
dst = strconv.AppendUint(dst, f.uint64value1, b10)
case typeUint64Ptr:
value, ok := f.interfacevalue1.(*uint64)
if ok && value != nil {
dst = strconv.AppendUint(dst, *value, b10)
break
}
dst = append(dst, null...)
case typeInt:
dst = strconv.AppendInt(dst, f.int64value1, b10)
case typeIntPtr:
value, ok := f.interfacevalue1.(*int)
if ok && value != nil {
dst = strconv.AppendInt(dst, int64(*value), b10)
break
}
dst = append(dst, null...)
case typeInt8:
dst = strconv.AppendInt(dst, f.int64value1, b10)
case typeInt8Ptr:
value, ok := f.interfacevalue1.(*int8)
if ok && value != nil {
dst = strconv.AppendInt(dst, int64(*value), b10)
break
}
dst = append(dst, null...)
case typeInt16:
dst = strconv.AppendInt(dst, f.int64value1, b10)
case typeInt16Ptr:
value, ok := f.interfacevalue1.(*int16)
if ok && value != nil {
dst = strconv.AppendInt(dst, int64(*value), b10)
break
}
dst = append(dst, null...)
case typeInt32:
dst = strconv.AppendInt(dst, f.int64value1, b10)
case typeInt32Ptr:
value, ok := f.interfacevalue1.(*int32)
if ok && value != nil {
dst = strconv.AppendInt(dst, int64(*value), b10)
break
}
dst = append(dst, null...)
case typeInt64:
dst = strconv.AppendInt(dst, f.int64value1, b10)
case typeInt64Ptr:
value, ok := f.interfacevalue1.(*int64)
if ok && value != nil {
dst = strconv.AppendInt(dst, *value, b10)
break
}
dst = append(dst, null...)
case typeFloat32:
dst = appendFloatFieldValue(dst, f.float64value1, b32)
case typeFloat32Ptr:
value, ok := f.interfacevalue1.(*float32)
if ok && value != nil {
dst = appendFloatFieldValue(dst, float64(*value), b32)
break
}
dst = append(dst, null...)
case typeFloat64:
dst = appendFloatFieldValue(dst, f.float64value1, b64)
case typeFloat64Ptr:
value, ok := f.interfacevalue1.(*float64)
if ok && value != nil {
dst = appendFloatFieldValue(dst, *value, b64)
break
}
dst = append(dst, null...)
// string
case typeComplex64:
dst = append(dst, '"')
dst = strconv.AppendFloat(dst, f.float64value1, 'f', -1, b32)
if f.float64value2 >= 0 && !math.IsInf(f.float64value2, b32) {
dst = append(dst, '+')
}
dst = strconv.AppendFloat(dst, f.float64value2, 'f', -1, b32)
dst = append(dst, 'i')
dst = append(dst, '"')
case typeComplex64Ptr:
value, ok := f.interfacevalue1.(*complex64)
if ok && value != nil {
dst = append(dst, '"')
dst = strconv.AppendFloat(dst, float64(real(*value)), 'f', -1, b32)
imaginary := imag(*value)
if imaginary >= 0 && !math.IsInf(float64(imaginary), b32) {
dst = append(dst, '+')
}
dst = strconv.AppendFloat(dst, float64(imaginary), 'f', -1, b32)
dst = append(dst, 'i')
dst = append(dst, '"')
break
}
dst = append(dst, null...)
case typeComplex128:
dst = append(dst, '"')
dst = strconv.AppendFloat(dst, f.float64value1, 'f', -1, b64)
if f.float64value2 >= 0 && !math.IsInf(f.float64value2, b64) {
dst = append(dst, '+')
}
dst = strconv.AppendFloat(dst, f.float64value2, 'f', -1, b64)
dst = append(dst, 'i')
dst = append(dst, '"')
case typeComplex128Ptr:
value, ok := f.interfacevalue1.(*complex128)
if ok && value != nil {
dst = append(dst, '"')
dst = strconv.AppendFloat(dst, real(*value), 'f', -1, b64)
imaginary := imag(*value)
if imaginary >= 0 && !math.IsInf(imaginary, b64) {
dst = append(dst, '+')
}
dst = strconv.AppendFloat(dst, imaginary, 'f', -1, b64)
dst = append(dst, 'i')
dst = append(dst, '"')
break
}
dst = append(dst, null...)
case typeTime:
value, _ := f.interfacevalue1.(*time.Location)
if value == nil {
value = time.UTC
}
dst = appendTimeFieldValue(dst, time.Unix(f.int64value1, f.int64value2).In(value), CustomTimeFormat)
case typeTimeFormat:
value, _ := f.interfacevalue1.(*time.Location)
if value == nil {
value = time.UTC
}
dst = appendTimeFieldValue(dst, time.Unix(f.int64value1, f.int64value2).In(value), f.stringvalue1)
case typeTimePtr:
value, ok := f.interfacevalue1.(*time.Time)
if ok && value != nil {
dst = appendTimeFieldValue(dst, *value, CustomTimeFormat)
break
}
dst = append(dst, null...)
case typeTimeFormatPtr:
value, ok := f.interfacevalue1.(*time.Time)
if ok && value != nil {
dst = appendTimeFieldValue(dst, *value, f.stringvalue1)
break
}
dst = append(dst, null...)
case typeDuration:
dst = strconv.AppendInt(dst, f.int64value2/f.int64value1, b10)
case typeDurationPtr:
value, ok := f.interfacevalue1.(*time.Duration)
if ok && value != nil {
dst = strconv.AppendInt(dst, int64(*value)/f.int64value1, b10)
break
}
dst = append(dst, null...)
case typeDurationFormat:
dst = append(appendJSONEscapedString(append(dst, '"'), f.stringvalue1), '"')
case typeDurationFormatPtr:
value, ok := f.interfacevalue1.(*time.Duration)
if ok && value != nil {
dst = append(appendJSONEscapedString(append(dst, '"'), value.String()), '"')
break
}
dst = append(dst, null...)
case typeString:
dst = append(appendJSONEscapedString(append(dst, '"'), f.stringvalue1), '"')
case typeStringPtr:
value, ok := f.interfacevalue1.(*string)
if ok && value != nil {
dst = append(appendJSONEscapedString(append(dst, '"'), *value), '"')
break
}
dst = append(dst, null...)
case typeStrings:
value, ok := f.interfacevalue1.([]string)
if ok && value != nil {
dst = append(dst, '[')
for _, v := range value {
dst = append(append(appendJSONEscapedString(append(dst, '"'), v), '"'), ',')
}
if dst[len(dst)-1] == ',' {
dst[len(dst)-1] = ']'
}
if dst[len(dst)-1] != ']' {
dst = append(dst, ']')
}
break
}
dst = append(dst, null...)
case typeStringer:
value, ok := f.interfacevalue1.(fmt.Stringer)
if ok && value != nil {
dst = append(appendJSONEscapedString(append(dst, '"'), value.String()), '"')
break
}
dst = append(dst, null...)
case typeFormatter:
value, ok := f.interfacevalue1.(fmt.Formatter)
if ok && value != nil {
dst = append(appendJSONEscapedString(append(dst, '"'), fmt.Sprintf("%+v", value)), '"')
break
}
dst = append(dst, null...)
case typeError:
value, ok := f.interfacevalue1.(error)
if ok && value != nil {
dst = append(appendJSONEscapedString(append(dst, '"'), value.Error()), '"')
break
}
dst = append(dst, null...)
case typeErrors:
value, ok := f.interfacevalue1.([]error)
if ok && value != nil {
dst = append(dst, '[')
for _, v := range value {
dst = append(append(appendJSONEscapedString(append(dst, '"'), v.Error()), '"'), ',')
}
if dst[len(dst)-1] == ',' {
dst[len(dst)-1] = ']'
}
if dst[len(dst)-1] != ']' {
dst = append(dst, ']')
}
break
}
dst = append(dst, null...)
case typeErrorStacktrace:
value, ok := f.interfacevalue1.(fmt.Formatter)
if ok && value != nil {
dst = append(appendJSONEscapedString(append(dst, '"'), fmt.Sprintf("%+v", value)), '"')
break
}
err, ok := f.interfacevalue1.(error)
if ok && err != nil {
dst = append(appendJSONEscapedString(append(dst, '"'), err.Error()), '"')
break
}
dst = append(dst, null...)
case typeInterface:
value := fmt.Sprintf("%+v", f.interfacevalue1)
if value != "<nil>" {
dst = append(appendJSONEscapedString(append(dst, '"'), value), '"')
break
}
dst = append(dst, null...)
case typeObject:
b, err := jsonMarshalFn(f.interfacevalue1)
if err != nil {
const skip = 4
defaultLogger.AddCallerSkip(skip).Error("rec.Object: json.Marshal: "+err.Error(), Error(err))
dst = append(dst, null...)
break
}
dst = append(dst, b...)
// abnormal
case typeNone:
dst = append(dst, `"ERROR: TYPE NONE"`...)
default:
dst = append(strconv.AppendInt(append(dst, `"ERROR: UNDEFINED TYPE: `...), int64(f.t), b10), '"')
}
return dst
}
// Bool returns rec.Field for bool type.
func Bool(key string, value bool) Field {
return Field{
t: typeBool,
key: key,
boolvalue1: value,
}
}
// BoolPtr returns rec.Field for *bool type.
func BoolPtr(key string, value *bool) Field {
return Field{
t: typeBoolPtr,
key: key,
interfacevalue1: value,
}
}
// Uint returns rec.Field for uint type.
func Uint(key string, value uint) Field {
return Field{
t: typeUint,
key: key,
uint64value1: uint64(value),
}
}
// UintPtr returns rec.Field for *uint type.
func UintPtr(key string, value *uint) Field {
return Field{
t: typeUintPtr,
key: key,
interfacevalue1: value,
}
}
// Uint8 returns rec.Field for uint8 type.
func Uint8(key string, value uint8) Field {
return Field{
t: typeUint8,
key: key,
uint64value1: uint64(value),
}
}
// Uint8Ptr returns rec.Field for *uint8 type.
func Uint8Ptr(key string, value *uint8) Field {
return Field{
t: typeUint8Ptr,
key: key,
interfacevalue1: value,
}
}
// Uint16 returns rec.Field for uint16 type.
func Uint16(key string, value uint16) Field {
return Field{
t: typeUint16,
key: key,
uint64value1: uint64(value),
}
}
// Uint16Ptr returns rec.Field for *uint16 type.
func Uint16Ptr(key string, value *uint16) Field {
return Field{
t: typeUint16Ptr,
key: key,
interfacevalue1: value,
}
}
// Uint32 returns rec.Field for uint32 type.
func Uint32(key string, value uint32) Field {
return Field{
t: typeUint32,
key: key,
uint64value1: uint64(value),
}
}
// Uint32Ptr returns rec.Field for *uint32 type.
func Uint32Ptr(key string, value *uint32) Field {
return Field{
t: typeUint32Ptr,
key: key,
interfacevalue1: value,
}
}
// Uint64 returns rec.Field for uint64 type.
func Uint64(key string, value uint64) Field {
return Field{
t: typeUint64,
key: key,
uint64value1: value,
}
}
// Uint64Ptr returns rec.Field for *uint64 type.
func Uint64Ptr(key string, value *uint64) Field {
return Field{
t: typeUint64Ptr,
key: key,
interfacevalue1: value,
}
}
// Int returns rec.Field for int type.
func Int(key string, value int) Field {
return Field{
t: typeInt,
key: key,
int64value1: int64(value),
}
}
// IntPtr returns rec.Field for *int type.
func IntPtr(key string, value *int) Field {
return Field{
t: typeIntPtr,
key: key,
interfacevalue1: value,
}
}
// Int8 returns rec.Field for int8 type.
func Int8(key string, value int8) Field {
return Field{
t: typeInt8,
key: key,
int64value1: int64(value),
}
}
// Int8Ptr returns rec.Field for *int8 type.
func Int8Ptr(key string, value *int8) Field {
return Field{
t: typeInt8Ptr,
key: key,
interfacevalue1: value,
}
}
// Int16 returns rec.Field for int16 type.
func Int16(key string, value int16) Field {
return Field{
t: typeInt16,
key: key,
int64value1: int64(value),
}
}
// Int16Ptr returns rec.Field for *int16 type.
func Int16Ptr(key string, value *int16) Field {
return Field{
t: typeInt16Ptr,
key: key,
interfacevalue1: value,
}
}
// Int32 returns rec.Field for int32 type.
func Int32(key string, value int32) Field {
return Field{
t: typeInt32,
key: key,
int64value1: int64(value),
}
}
// Int32Ptr returns rec.Field for *int32 type.
func Int32Ptr(key string, value *int32) Field {
return Field{
t: typeInt32Ptr,
key: key,
interfacevalue1: value,
}
}
// Int64 returns rec.Field for int64 type.
func Int64(key string, value int64) Field {
return Field{
t: typeInt64,
key: key,
int64value1: value,
}
}
// Int64Ptr returns rec.Field for *int64 type.
func Int64Ptr(key string, value *int64) Field {
return Field{
t: typeInt64Ptr,
key: key,
interfacevalue1: value,
}
}
// Float32 returns rec.Field for float32 type.
func Float32(key string, value float32) Field {
return Field{
t: typeFloat32,
key: key,
float64value1: float64(value),
}
}
// Float32Ptr returns rec.Field for *float32 type.
func Float32Ptr(key string, value *float32) Field {
return Field{
t: typeFloat32Ptr,
key: key,
interfacevalue1: value,
}
}
// Float64 returns rec.Field for float64 type.
func Float64(key string, value float64) Field {
return Field{
t: typeFloat64,
key: key,
float64value1: value,
}
}
// Float64Ptr returns rec.Field for *float64 type.
func Float64Ptr(key string, value *float64) Field {
return Field{
t: typeFloat64Ptr,
key: key,
interfacevalue1: value,
}
}
// Complex64 returns rec.Field for complex64 type.
func Complex64(key string, value complex64) Field {
return Field{
t: typeComplex64,
key: key,
float64value1: float64(real(value)),
float64value2: float64(imag(value)),
}
}
// Complex64Ptr returns rec.Field for *complex128 type.
func Complex64Ptr(key string, value *complex64) Field {
return Field{
t: typeComplex64Ptr,
key: key,
interfacevalue1: value,
}
}
// Complex128 returns rec.Field for complex128 type.
func Complex128(key string, value complex128) Field {
return Field{
t: typeComplex128,
key: key,
float64value1: real(value),
float64value2: imag(value),
}
}
// Complex128Ptr returns rec.Field for *complex128 type.
func Complex128Ptr(key string, value *complex128) Field {
return Field{
t: typeComplex128Ptr,
key: key,
interfacevalue1: value,
}
}
// Time returns rec.Field for time.Time type.
func Time(key string, value time.Time) Field {
return Field{
t: typeTime,
key: key,
interfacevalue1: value.Location(),
int64value1: value.Unix(),
int64value2: int64(value.Nanosecond()),
}
}
// TimeFormat returns rec.Field for time.Time type with time format.
func TimeFormat(key string, format string, value time.Time) Field {
return Field{
t: typeTimeFormat,
key: key,
interfacevalue1: value.Location(),
int64value1: value.Unix(),
int64value2: int64(value.Nanosecond()),
stringvalue1: format,
}
}
// TimePtr returns rec.Field for *time.Time type.
func TimePtr(key string, value *time.Time) Field {
return Field{
t: typeTimePtr,
key: key,
interfacevalue1: value,
}
}
// TimeFormatPtr returns rec.Field for *time.Time type with time format.
func TimeFormatPtr(key string, format string, value *time.Time) Field {
return Field{
t: typeTimeFormatPtr,
key: key,
interfacevalue1: value,
stringvalue1: format,
}
}
// Duration returns rec.Field for time.Duration type.
func Duration(key string, unit time.Duration, value time.Duration) Field {
return Field{
t: typeDuration,
key: key,
int64value1: int64(unit),
int64value2: int64(value),
}
}
// DurationPtr returns rec.Field for time.Duration type.
func DurationPtr(key string, unit time.Duration, value *time.Duration) Field {
return Field{
t: typeDurationPtr,
key: key,
int64value1: int64(unit),
interfacevalue1: value,
}
}
// DurationFormat returns rec.Field for time.Duration type with duration format.
func DurationFormat(key string, value time.Duration) Field {
return Field{
t: typeDurationFormat,
key: key,
stringvalue1: value.String(),
}
}
// DurationFormatPtr returns rec.Field for time.Duration type with duration format.
func DurationFormatPtr(key string, value *time.Duration) Field {
return Field{
t: typeDurationFormatPtr,
key: key,
interfacevalue1: value,
}
}
// String returns rec.Field for string type.
func String(key string, value string) Field {
return Field{
t: typeString,
key: key,
stringvalue1: value,
}
}
// Sprintf returns rec.Field like fmt.Sprintf() for string type.
func Sprintf(key string, format string, a ...interface{}) Field {
return Field{
t: typeString,
key: key,
stringvalue1: fmt.Sprintf(format, a...),
}
}
// StringPtr returns rec.Field like fmt.Sprintf() for *string type.
func StringPtr(key string, value *string) Field {
return Field{
t: typeStringPtr,
key: key,
interfacevalue1: value,
}
}
// Strings returns rec.Field for []string type.
func Strings(key string, value []string) Field {
return Field{
t: typeStrings,
key: key,
interfacevalue1: value,
}
}
// Stringer returns rec.Field for fmt.Stringer type.
func Stringer(key string, value fmt.Stringer) Field {
return Field{
t: typeStringer,
key: key,
interfacevalue1: value,
}
}
// Formatter returns rec.Field for fmt.Formatter type.
func Formatter(key string, value fmt.Formatter) Field {
return Field{
t: typeFormatter,
key: key,
interfacevalue1: value,
}
}
// Error returns rec.Field for error type.
// Field key is fixed to `error`.
// If you want to use other field key, use rec.ErrorWithKey().
func Error(err error) Field {
return ErrorWithKey(errorKey, err)
}
// ErrorWithKey returns rec.Field for error type.
func ErrorWithKey(key string, err error) Field {
return Field{
t: typeError,
key: key,
interfacevalue1: err,
}
}
// Errors returns rec.Field for []error type.
// Field key is fixed to `errors`.
// If you want to use other field key, use rec.ErrorsWithKey().
func Errors(errs []error) Field {
return ErrorsWithKey(errorsKey, errs)
}
// ErrorsWithKey returns rec.Field for []error type.
func ErrorsWithKey(key string, errs []error) Field {
return Field{
t: typeErrors,
key: key,
interfacevalue1: errs,
}
}
// ErrorStacktrace returns rec.Field for error type with stacktrace.
// Field key is fixed to `errorStacktrace`.
// If you want to use other field key, use rec.ErrorStacktraceWithKey().
func ErrorStacktrace(err error) Field {
return ErrorStacktraceWithKey(errorStacktraceKey, err)
}
// ErrorStacktraceWithKey returns rec.Field for error type with stacktrace.
func ErrorStacktraceWithKey(key string, err error) Field {
return Field{
t: typeErrorStacktrace,
key: key,
interfacevalue1: err,
}
}
// Interface returns rec.Field for interface{} type.
func Interface(key string, value interface{}) Field {
return Field{
t: typeInterface,
key: key,
interfacevalue1: value,
}
}
// Object returns a rec.Field for the JSON Object struct as shown below:
//
// type Example {
// Key string `json:"key"`
// Value string `json:"value"`
// }
func Object(key string, object interface{}) Field {
return Field{
t: typeObject,
key: key,
interfacevalue1: object,
}
}