/
field.go
401 lines (347 loc) · 11.2 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
package logging
import (
"context"
"fmt"
"time"
)
type FieldType uint8
const (
// UnknownType is the default field type.
UnknownType FieldType = iota
// BinaryType indicates that the field carries an opaque binary blob.
BinaryType
// BoolType indicates that the field carries a bool.
BoolType
// Complex128Type indicates that the field carries a complex128.
Complex128Type
// Complex64Type indicates that the field carries a complex128.
Complex64Type
// DurationType indicates that the field carries a time.Duration.
DurationType
// Float64Type indicates that the field carries a float64.
Float64Type
// Float32Type indicates that the field carries a float32.
Float32Type
// Int64Type indicates that the field carries an int64.
Int64Type
// Int32Type indicates that the field carries an int32.
Int32Type
// Int16Type indicates that the field carries an int16.
Int16Type
// Int8Type indicates that the field carries an int8.
Int8Type
// StringType indicates that the field carries a string.
StringType
// TimeType indicates that the field carries a time.Time.
TimeType
// Uint64Type indicates that the field carries an uint64.
Uint64Type
// Uint32Type indicates that the field carries an uint32.
Uint32Type
// Uint16Type indicates that the field carries an uint16.
Uint16Type
// Uint8Type indicates that the field carries an uint8.
Uint8Type
// UintptrType indicates that the field carries an uintptr.
UintptrType
// StringerType indicates that the field carries a fmt.Stringer.
StringerType
// ErrorType indicates that the field carries an error.
ErrorType
// StackType indicates that the field captures stacktrace of the current goroutine.
StackType
)
// Field is logging field.
type Field interface {
// Key is field key.
Key() string
// Type is field type.
Type() FieldType
// Value is field value.
Value() any
}
type field struct {
key string
typ FieldType
val any
}
func (f field) Key() string { return f.key }
func (f field) Type() FieldType { return f.typ }
func (f field) Value() any { return f.val }
// Any creates a Field of UnknownType value.
func Any(key string, value any) Field {
return field{key: key, typ: UnknownType, val: value}
}
// Binary creates a Field of BinaryType value.
func Binary(key string, value []byte) Field {
val := make([]byte, len(value))
copy(val, value)
return field{key: key, typ: BinaryType, val: val}
}
// Bool creates a Field of BoolType value.
func Bool(key string, value bool) Field {
return field{key: key, typ: BoolType, val: value}
}
// Boolp creates a Field of BoolType value if value is not nil, or a Field of UnknownType value if value is nil.
func Boolp(key string, value *bool) Field {
if value == nil {
return Any(key, value)
}
return Bool(key, *value)
}
// Complex128 creates a Field of Complex128Type value.
func Complex128(key string, value complex128) Field {
return field{key: key, typ: Complex128Type, val: value}
}
// Complex128p creates a Field of Complex128Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Complex128p(key string, value *complex128) Field {
if value == nil {
return Any(key, value)
}
return Complex128(key, *value)
}
// Complex64 creates a Field of Complex64Type value.
func Complex64(key string, value complex64) Field {
return field{key: key, typ: Complex64Type, val: value}
}
// Complex64p creates a Field of Complex64Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Complex64p(key string, value *complex64) Field {
if value == nil {
return Any(key, value)
}
return Complex64(key, *value)
}
// Duration creates a Field of DurationType value.
func Duration(key string, value time.Duration) Field {
return field{key: key, typ: DurationType, val: value}
}
// Durationp creates a Field of DurationType value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Durationp(key string, value *time.Duration) Field {
if value == nil {
return Any(key, value)
}
return Duration(key, *value)
}
// Float64 creates a Field of Float64Type value.
func Float64(key string, value float64) Field {
return field{key: key, typ: Float64Type, val: value}
}
// Float64p creates a Field of Float64Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Float64p(key string, value *float64) Field {
if value == nil {
return Any(key, value)
}
return Float64(key, *value)
}
// Float32 creates a Field of Float32Type value.
func Float32(key string, value float32) Field {
return field{key: key, typ: Float32Type, val: value}
}
// Float32p creates a Field of Float32Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Float32p(key string, value *float32) Field {
if value == nil {
return Any(key, value)
}
return Float32(key, *value)
}
// Int creates a Field of Int64Type value.
func Int(key string, value int) Field {
return field{key: key, typ: Int64Type, val: int64(value)}
}
// Intp creates a Field of Int64Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Intp(key string, value *int) Field {
if value == nil {
return Any(key, (*int64)(nil))
}
return Int(key, *value)
}
// Int64 creates a Field of Int64Type value.
func Int64(key string, value int64) Field {
return field{key: key, typ: Int64Type, val: value}
}
// Int64p creates a Field of Int64Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Int64p(key string, value *int64) Field {
if value == nil {
return Any(key, value)
}
return Int64(key, *value)
}
// Int32 creates a Field of Int32Type value.
func Int32(key string, value int32) Field {
return field{key: key, typ: Int32Type, val: value}
}
// Int32p creates a Field of Int32Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Int32p(key string, value *int32) Field {
if value == nil {
return Any(key, value)
}
return Int32(key, *value)
}
// Int16 creates a Field of Int16Type value.
func Int16(key string, value int16) Field {
return field{key: key, typ: Int16Type, val: value}
}
// Int16p creates a Field of Int16Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Int16p(key string, value *int16) Field {
if value == nil {
return Any(key, value)
}
return Int16(key, *value)
}
// Int8 creates a Field of Int8Type value.
func Int8(key string, value int8) Field {
return field{key: key, typ: Int8Type, val: value}
}
// Int8p creates a Field of Int8Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Int8p(key string, value *int8) Field {
if value == nil {
return Any(key, value)
}
return Int8(key, *value)
}
// String creates a Field of StringType value.
func String(key string, value string) Field {
return field{key: key, typ: StringType, val: value}
}
// Stringp creates a Field of StringType value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Stringp(key string, value *string) Field {
if value == nil {
return Any(key, value)
}
return String(key, *value)
}
// Time creates a Field of TimeType value.
func Time(key string, value time.Time) Field {
return field{key: key, typ: TimeType, val: value}
}
// Timep creates a Field of TimeType value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Timep(key string, value *time.Time) Field {
if value == nil {
return Any(key, value)
}
return Time(key, *value)
}
// Uint creates a Field of Uint64Type value.
func Uint(key string, value uint) Field {
return field{key: key, typ: Uint64Type, val: uint64(value)}
}
// Uintp creates a Field of Uint64Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Uintp(key string, value *uint) Field {
if value == nil {
return Any(key, (*uint64)(nil))
}
return Uint(key, *value)
}
// Uint64 creates a Field of Uint64Type value.
func Uint64(key string, value uint64) Field {
return field{key: key, typ: Uint64Type, val: value}
}
// Uint64p creates a Field of Uint64Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Uint64p(key string, value *uint64) Field {
if value == nil {
return Any(key, value)
}
return Uint64(key, *value)
}
// Uint32 creates a Field of Uint32Type value.
func Uint32(key string, value uint32) Field {
return field{key: key, typ: Uint32Type, val: value}
}
// Uint32p creates a Field of Uint32Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Uint32p(key string, value *uint32) Field {
if value == nil {
return Any(key, value)
}
return Uint32(key, *value)
}
// Uint16 creates a Field of Uint16Type value.
func Uint16(key string, value uint16) Field {
return field{key: key, typ: Uint16Type, val: value}
}
// Uint16p creates a Field of Uint16Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Uint16p(key string, value *uint16) Field {
if value == nil {
return Any(key, value)
}
return Uint16(key, *value)
}
// Uint8 creates a Field of Uint8Type value.
func Uint8(key string, value uint8) Field {
return field{key: key, typ: Uint8Type, val: value}
}
// Uint8p creates a Field of Uint8Type value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Uint8p(key string, value *uint8) Field {
if value == nil {
return Any(key, value)
}
return Uint8(key, *value)
}
// Uintptr creates a Field of UintptrType value.
func Uintptr(key string, value uintptr) Field {
return field{key: key, typ: UintptrType, val: value}
}
// Uintptrp creates a Field of UintptrType value if value is not nil,
// or a Field of UnknownType value if value is nil.
func Uintptrp(key string, value *uintptr) Field {
if value == nil {
return Any(key, value)
}
return Uintptr(key, *value)
}
// Stringer creates a Field of StringerType value.
func Stringer(key string, value fmt.Stringer) Field {
return field{key: key, typ: StringerType, val: value}
}
// Error creates a Field of ErrorType value, with key "error".
func Error(err error) Field {
return field{key: "error", typ: ErrorType, val: err}
}
// NamedError create a Field of ErrorType value.
func NamedError(key string, err error) Field {
return field{key: key, typ: ErrorType, val: err}
}
// Stack create a Field that captures stacktrace of the current goroutine.
func Stack(key string) Field {
return field{key: key, typ: StackType, val: 0}
}
// StackSkip create a Field that captures stacktrace of the current goroutine,
// skipping the given number of frames from the top of the stacktrace.
func StackSkip(key string, skip int) Field {
return field{key: key, typ: StackType, val: skip}
}
type fieldKey struct{}
// NewContext wraps fields into a new context and return it.
func NewContext(ctx context.Context, field ...Field) context.Context {
fields, _ := ctx.Value(fieldKey{}).([]Field)
fields = append(fields, field...)
return context.WithValue(ctx, fieldKey{}, fields)
}
// WithContextField tries extract fields from context and returns a Logger with these fields.
// If no fields found, the origin logger is returned.
func WithContextField(ctx context.Context, logger Logger) Logger {
fields, ok := ctx.Value(fieldKey{}).([]Field)
if !ok {
return logger
}
if len(fields) == 0 {
return logger
}
return logger.WithField(fields...)
}