This repository has been archived by the owner on Jun 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
field.go
390 lines (337 loc) · 9.52 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
package rz
import (
"net"
"time"
)
// Field functions are used to add fields to events
type Field func(e *Event)
// Discard disables the event
func Discard() Field {
return func(e *Event) {
e.discard()
}
}
// // Array adds the field key with an array to the event context.
// // Use Event.Arr() to create the array or pass a type that
// // implement the LogArrayMarshaler interface.
// func Array(key string, arr logArrayMarshaler) Field {
// return func(e *Event) {
// e.array(key, arr)
// }
// }
// Stack enables stack trace printing for the error passed to Err().
//
// logger.errorStackMarshaler must be set for this method to do something.
func Stack(enable bool) Field {
return func(e *Event) {
e.enableStack(enable)
}
}
// Caller adds the file:line of the caller with the rz.CallerFieldName key.
func Caller(enable bool) Field {
return func(e *Event) {
e.enableCaller(enable)
}
}
// Map is a helper function to use a map to set fields using type assertion.
func Map(fields map[string]interface{}) Field {
return func(e *Event) {
e.fields(fields)
}
}
// String adds the field key with val as a string to the *Event context.
func String(key, value string) Field {
return func(e *Event) {
e.string(key, value)
}
}
// Strings adds the field key with vals as a []string to the *Event context.
func Strings(key string, value []string) Field {
return func(e *Event) {
e.strings(key, value)
}
}
// Time adds the field key with t formated as string using rz.TimeFieldFormat.
func Time(key string, value time.Time) Field {
return func(e *Event) {
e.time(key, value)
}
}
// Times adds the field key with t formated as string using rz.TimeFieldFormat.
func Times(key string, value []time.Time) Field {
return func(e *Event) {
e.times(key, value)
}
}
// Duration adds the field key with duration d stored as rz.DurationFieldUnit.
// If rz.DurationFieldInteger is true, durations are rendered as integer
// instead of float.
func Duration(key string, value time.Duration) Field {
return func(e *Event) {
e.duration(key, value)
}
}
// Durations adds the field key with duration d stored as rz.DurationFieldUnit.
// If rz.DurationFieldInteger is true, durations are rendered as integer
// instead of float.
func Durations(key string, value []time.Duration) Field {
return func(e *Event) {
e.durations(key, value)
}
}
// Object marshals an object that implement the LogObjectMarshaler interface.
func Object(key string, value LogObjectMarshaler) Field {
return func(e *Event) {
e.object(key, value)
}
}
// EmbedObject marshals an object that implement the LogObjectMarshaler interface.
func EmbedObject(obj LogObjectMarshaler) Field {
return func(e *Event) {
e.embedObject(obj)
}
}
// Dict adds the field key with a dict to the event context.
// Use rz.Dict() to create the dictionary.
func Dict(key string, value *Event) Field {
return func(e *Event) {
e.dict(key, value)
}
}
// Bytes adds the field key with val as a string to the *Event context.
//
// Runes outside of normal ASCII ranges will be hex-encoded in the resulting
// JSON.
func Bytes(key string, value []byte) Field {
return func(e *Event) {
e.bytes(key, value)
}
}
// Bool adds the field key with i as a bool to the *Event context.
func Bool(key string, value bool) Field {
return func(e *Event) {
e.bool(key, value)
}
}
// Bools adds the field key with i as a []bool to the *Event context.
func Bools(key string, value []bool) Field {
return func(e *Event) {
e.bools(key, value)
}
}
// Any adds the field key with i marshaled using reflection.
func Any(key string, value interface{}) Field {
return func(e *Event) {
e.iinterface(key, value)
}
}
// IP adds IPv4 or IPv6 Address to the event
func IP(key string, value net.IP) Field {
return func(e *Event) {
e.ip(key, value)
}
}
// IPNet adds IPv4 or IPv6 Prefix (address and mask) to the event
func IPNet(key string, value net.IPNet) Field {
return func(e *Event) {
e.ipNet(key, value)
}
}
// HardwareAddr adds HardwareAddr to the event
func HardwareAddr(key string, value net.HardwareAddr) Field {
return func(e *Event) {
e.hardwareAddr(key, value)
}
}
// Timestamp adds the current local time as UNIX timestamp to the *Event context with the
// logger.TimestampFieldName key.
func Timestamp(enable bool) Field {
return func(e *Event) {
e.enableTimestamp(enable)
}
}
// Error adds the field key with serialized err to the *Event context.
// If err is nil, no field is added.
func Error(key string, value error) Field {
return func(e *Event) {
e.error(key, value)
}
}
// Err adds the field "error" with serialized err to the *Event context.
// If err is nil, no field is added.
// To customize the key name, uze rz.ErrorFieldName.
//
// If Stack() has been called before and rz.ErrorStackMarshaler is defined,
// the err is passed to ErrorStackMarshaler and the result is appended to the
// rz.ErrorStackFieldName.
func Err(value error) Field {
return func(e *Event) {
e.err(value)
}
}
// Errors adds the field key with errs as an array of serialized errors to the
// *Event context.
func Errors(key string, value []error) Field {
return func(e *Event) {
e.errors(key, value)
}
}
// Hex adds the field key with val as a hex string to the *Event context.
func Hex(key string, value []byte) Field {
return func(e *Event) {
e.hex(key, value)
}
}
// RawJSON adds already encoded JSON to the log line under key.
//
// No sanity check is performed on b; it must not contain carriage returns and
// be valid JSON.
func RawJSON(key string, value []byte) Field {
return func(e *Event) {
e.rawJSON(key, value)
}
}
// Int adds the field key with i as a int to the *Event context.
func Int(key string, value int) Field {
return func(e *Event) {
e.int(key, value)
}
}
// Ints adds the field key with i as a []int to the *Event context.
func Ints(key string, value []int) Field {
return func(e *Event) {
e.ints(key, value)
}
}
// Int8 adds the field key with i as a int8 to the *Event context.
func Int8(key string, value int8) Field {
return func(e *Event) {
e.int8(key, value)
}
}
// Ints8 adds the field key with i as a []int8 to the *Event context.
func Ints8(key string, value []int8) Field {
return func(e *Event) {
e.ints8(key, value)
}
}
// Int16 adds the field key with i as a int16 to the *Event context.
func Int16(key string, value int16) Field {
return func(e *Event) {
e.int16(key, value)
}
}
// Ints16 adds the field key with i as a []int16 to the *Event context.
func Ints16(key string, value []int16) Field {
return func(e *Event) {
e.ints16(key, value)
}
}
// Int32 adds the field key with i as a int32 to the *Event context.
func Int32(key string, value int32) Field {
return func(e *Event) {
e.int32(key, value)
}
}
// Ints32 adds the field key with i as a []int32 to the *Event context.
func Ints32(key string, value []int32) Field {
return func(e *Event) {
e.ints32(key, value)
}
}
// Int64 adds the field key with i as a int64 to the *Event context.
func Int64(key string, value int64) Field {
return func(e *Event) {
e.int64(key, value)
}
}
// Ints64 adds the field key with i as a []int64 to the *Event context.
func Ints64(key string, value []int64) Field {
return func(e *Event) {
e.ints64(key, value)
}
}
// Uint adds the field key with i as a uint to the *Event context.
func Uint(key string, value uint) Field {
return func(e *Event) {
e.uint(key, value)
}
}
// Uints adds the field key with i as a []uint to the *Event context.
func Uints(key string, value []uint) Field {
return func(e *Event) {
e.uints(key, value)
}
}
// Uint8 adds the field key with i as a uint8 to the *Event context.
func Uint8(key string, value uint8) Field {
return func(e *Event) {
e.uint8(key, value)
}
}
// Uints8 adds the field key with i as a []uint8 to the *Event context.
func Uints8(key string, value []uint8) Field {
return func(e *Event) {
e.uints8(key, value)
}
}
// Uint16 adds the field key with i as a uint16 to the *Event context.
func Uint16(key string, value uint16) Field {
return func(e *Event) {
e.uint16(key, value)
}
}
// Uints16 adds the field key with i as a []uint16 to the *Event context.
func Uints16(key string, value []uint16) Field {
return func(e *Event) {
e.uints16(key, value)
}
}
// Uint32 adds the field key with i as a uint32 to the *Event context.
func Uint32(key string, value uint32) Field {
return func(e *Event) {
e.uint32(key, value)
}
}
// Uints32 adds the field key with i as a []uint32 to the *Event context.
func Uints32(key string, value []uint32) Field {
return func(e *Event) {
e.uints32(key, value)
}
}
// Uint64 adds the field key with i as a uint64 to the *Event context.
func Uint64(key string, value uint64) Field {
return func(e *Event) {
e.uint64(key, value)
}
}
// Uints64 adds the field key with i as a []uint64 to the *Event context.
func Uints64(key string, value []uint64) Field {
return func(e *Event) {
e.uints64(key, value)
}
}
// Float32 adds the field key with f as a float32 to the *Event context.
func Float32(key string, value float32) Field {
return func(e *Event) {
e.float32(key, value)
}
}
// Floats32 adds the field key with f as a []float32 to the *Event context.
func Floats32(key string, value []float32) Field {
return func(e *Event) {
e.floats32(key, value)
}
}
// Float64 adds the field key with f as a float64 to the *Event context.
func Float64(key string, value float64) Field {
return func(e *Event) {
e.float64(key, value)
}
}
// Floats64 adds the field key with f as a []float64 to the *Event context.
func Floats64(key string, value []float64) Field {
return func(e *Event) {
e.floats64(key, value)
}
}