-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfields.go
More file actions
344 lines (292 loc) · 9.34 KB
/
fields.go
File metadata and controls
344 lines (292 loc) · 9.34 KB
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
package soba
import (
"fmt"
"strings"
"time"
)
// A Field is an operation that add a key-value pair to the logger's context.
// Most fields are lazily marshaled, so it's inexpensive to add fields to disabled debug-level log statements.
type Field struct {
name string
handler func(Encoder)
}
// Name returns field key.
func (field Field) Name() string {
return field.name
}
// Write marshaled current field to given encoder so its key-value pair will be available in logger's context.
func (field Field) Write(encoder Encoder) {
field.handler(encoder)
}
// NewField creates a new field.
func NewField(name string, handler func(Encoder)) Field {
return Field{
name: strings.ToLower(name),
handler: handler,
}
}
// ----------------------------------------------------------------------------
// Object
// ----------------------------------------------------------------------------
// Object creates a typesafe Field with given key and ObjectMarshaler.
func Object(key string, value ObjectMarshaler) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddObject(key, value)
})
}
// Int creates a typesafe Field with given key and int.
func Int(key string, value int) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddInt(key, value)
})
}
// Int8 creates a typesafe Field with given key and int8.
func Int8(key string, value int8) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddInt8(key, value)
})
}
// Int16 creates a typesafe Field with given key and int16.
func Int16(key string, value int16) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddInt16(key, value)
})
}
// Int32 creates a typesafe Field with given key and int32.
func Int32(key string, value int32) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddInt32(key, value)
})
}
// Int64 creates a typesafe Field with given key and int64.
func Int64(key string, value int64) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddInt64(key, value)
})
}
// Uint creates a typesafe Field with given key and uint.
func Uint(key string, value uint) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddUint(key, value)
})
}
// Uint8 creates a typesafe Field with given key and uint8.
func Uint8(key string, value uint8) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddUint8(key, value)
})
}
// Uint16 creates a typesafe Field with given key and uint16.
func Uint16(key string, value uint16) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddUint16(key, value)
})
}
// Uint32 creates a typesafe Field with given key and uint32.
func Uint32(key string, value uint32) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddUint32(key, value)
})
}
// Uint64 creates a typesafe Field with given key and uint64.
func Uint64(key string, value uint64) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddUint64(key, value)
})
}
// Float32 creates a typesafe Field with given key and float32.
func Float32(key string, value float32) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddFloat32(key, value)
})
}
// Float64 creates a typesafe Field with given key and float64.
func Float64(key string, value float64) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddFloat64(key, value)
})
}
// String creates a typesafe Field with given key and string.
func String(key, value string) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddString(key, value)
})
}
// Stringer creates a typesafe Field with given key and Stringer.
func Stringer(key string, value fmt.Stringer) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddStringer(key, value)
})
}
// Time creates a typesafe Field with given key and Time.
func Time(key string, value time.Time) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddTime(key, value)
})
}
// Duration creates a typesafe Field with given key and Duration.
func Duration(key string, value time.Duration) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddDuration(key, value)
})
}
// Bool creates a typesafe Field with given key and Bool.
func Bool(key string, value bool) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddBool(key, value)
})
}
// Binary creates a typesafe Field with given key and slice of byte.
func Binary(key string, value []byte) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddBinary(key, value)
})
}
// Skip is a no-op Field
func Skip(key string) Field {
return NewField(key, func(encoder Encoder) {})
}
// Error is an alias of NamedError("error", err).
func Error(err error) Field {
return NamedError("error", err)
}
// NamedError creates a typesafe Field with given key and error.
func NamedError(key string, err error) Field {
if err == nil {
return Skip(key)
}
return String(key, err.Error())
}
// Null creates a typesafe Field with given key as null value.
func Null(key string) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddNull(key)
})
}
// ----------------------------------------------------------------------------
// Array
// ----------------------------------------------------------------------------
// Objects creates a typesafe Field with given key and collection of ObjectMarshaler.
func Objects(key string, values []ObjectMarshaler) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddObjects(key, values)
})
}
// Array creates a typesafe Field with given key and ArrayMarshaler.
func Array(key string, value ArrayMarshaler) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddArray(key, value)
})
}
// Ints creates a typesafe Field with given key and slice of int.
func Ints(key string, values []int) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddInts(key, values)
})
}
// Int8s creates a typesafe Field with given key and slice of int8.
func Int8s(key string, values []int8) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddInt8s(key, values)
})
}
// Int16s creates a typesafe Field with given key and slice of int16.
func Int16s(key string, values []int16) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddInt16s(key, values)
})
}
// Int32s creates a typesafe Field with given key and slice of int32.
func Int32s(key string, values []int32) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddInt32s(key, values)
})
}
// Int64s creates a typesafe Field with given key and slice of int64.
func Int64s(key string, values []int64) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddInt64s(key, values)
})
}
// Uints creates a typesafe Field with given key and slice of uint.
func Uints(key string, values []uint) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddUints(key, values)
})
}
// Uint8s creates a typesafe Field with given key and slice of uint8.
func Uint8s(key string, values []uint8) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddUint8s(key, values)
})
}
// Uint16s creates a typesafe Field with given key and slice of uint16.
func Uint16s(key string, values []uint16) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddUint16s(key, values)
})
}
// Uint32s creates a typesafe Field with given key and slice of uint32.
func Uint32s(key string, values []uint32) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddUint32s(key, values)
})
}
// Uint64s creates a typesafe Field with given key and slice of uint64.
func Uint64s(key string, values []uint64) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddUint64s(key, values)
})
}
// Float32s creates a typesafe Field with given key and slice of float32.
func Float32s(key string, values []float32) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddFloat32s(key, values)
})
}
// Float64s creates a typesafe Field with given key and slice of float64.
func Float64s(key string, values []float64) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddFloat64s(key, values)
})
}
// Strings creates a typesafe Field with given key and slice of string.
func Strings(key string, values []string) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddStrings(key, values)
})
}
// Stringers creates a typesafe Field with given key and slice of Stringer.
func Stringers(key string, values []fmt.Stringer) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddStringers(key, values)
})
}
// Times creates a typesafe Field with given key and slice of Time.
func Times(key string, values []time.Time) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddTimes(key, values)
})
}
// Durations creates a typesafe Field with given key and slice of Duration.
func Durations(key string, values []time.Duration) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddDurations(key, values)
})
}
// Bools creates a typesafe Field with given key and slice of boolean.
func Bools(key string, values []bool) Field {
return NewField(key, func(encoder Encoder) {
encoder.AddBools(key, values)
})
}
// Errors creates a typesafe Field with given key and slice of error.
func Errors(key string, errors []error) Field {
return NewField(key, func(encoder Encoder) {
list := []string{}
for i := range errors {
list = append(list, errors[i].Error())
}
encoder.AddStrings(key, list)
})
}