-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfields.go
289 lines (229 loc) · 7.39 KB
/
fields.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
package frog
import (
"path/filepath"
"strconv"
"time"
)
type Field struct {
Name string
Value string
IsJSONString bool // if true, the string in Value should be bookended by double quotes to be valid JSON
IsJSONSafe bool // if true, this string only contains alpha-numerics, spaces, and safe punctuation
}
// Fielder is an interface used to add structured logging to calls to Logger methods
type Fielder interface {
Field() Field
}
// Fieldify takes a slice of Fielders and calls Field() on each, resulting in a slice of Fields
func Fieldify(f []Fielder) []Field {
fields := make([]Field, len(f))
for i := range f {
fields[i] = f[i].Field()
}
return fields
}
// FieldifyAndAppend returns a slice of Fields that starts with a copy the passed in []Field,
// and then appends the passed in []Fielder, after first rendering them to Fields.
func FieldifyAndAppend(fields []Field, fielders []Fielder) []Field {
var out []Field
if len(fielders)+len(fields) > 0 {
out = make([]Field, len(fields), len(fielders)+len(fields))
copy(out, fields)
for _, fielder := range fielders {
out = append(out, fielder.Field())
}
}
return out
}
// Bool adds a field whose value will be true or false
func Bool(name string, value bool) FieldBool {
return FieldBool{Name: name, Value: value}
}
// Byte adds an 8-bit unsigned integer field
func Byte(name string, value byte) FieldUint64 {
return FieldUint64{Name: name, Value: uint64(value)}
}
// Dur adds a time.Duration field
func Dur(name string, value time.Duration) FieldDuration {
return Duration(name, value)
}
// Duration adds a time.Duration field
func Duration(name string, value time.Duration) FieldDuration {
return FieldDuration{Name: name, Value: value}
}
// Err adds an error field named "error"
func Err(value error) FieldError {
return FieldError{Name: "error", Value: value}
}
// Float32 adds a 32-bit floating point number field
func Float32(name string, value float32) FieldFloat32 {
return FieldFloat32{Name: name, Value: value}
}
// Float64 adds a 64-bit floating point number field
func Float64(name string, value float64) FieldFloat64 {
return FieldFloat64{Name: name, Value: value}
}
// Int adds a signed integer field
func Int(name string, value int) FieldInt64 {
return FieldInt64{Name: name, Value: int64(value)}
}
// Int8 adds an 8-bit signed integer field
func Int8(name string, value int8) FieldInt64 {
return FieldInt64{Name: name, Value: int64(value)}
}
// Int16 adds a 16-bit signed integer field
func Int16(name string, value int16) FieldInt64 {
return FieldInt64{Name: name, Value: int64(value)}
}
// Int32 adds a 32-bit signed integer field
func Int32(name string, value int32) FieldInt64 {
return FieldInt64{Name: name, Value: int64(value)}
}
// Int64 adds a 64-bit signed integer field
func Int64(name string, value int64) FieldInt64 {
return FieldInt64{Name: name, Value: value}
}
// Path adds a field named "path" with the value of the passed in path, with '/' as
// the path separator ('/' is valid on Windows, and avoids escaping '\\' characters)
func Path(path string) FieldString {
return FieldString{Name: "path", Value: filepath.ToSlash(path)}
}
// PathAbs adds a field named "path_abs" that contains the result of passing the given
// path to filepath.Abs(). Similar to Path, '/' is used as the path separator.
func PathAbs(path string) FieldString {
abs, _ := filepath.Abs(path)
return FieldString{Name: "path_abs", Value: filepath.ToSlash(abs)}
}
// String adds an escaped and quoted string field
func String(name string, value string) FieldString {
return FieldString{Name: name, Value: value}
}
// Time adds a time.Time field that will output a string formatted using RFC 3339 (ISO 8601)
func Time(name string, value time.Time) FieldTimeFormat {
return FieldTimeFormat{Name: name, Value: value, Format: time.RFC3339}
}
// TimeNano adds a time.Time field that will output a string formatted using RFC 3339 with nanosecond precision
func TimeNano(name string, value time.Time) FieldTimeFormat {
return FieldTimeFormat{Name: name, Value: value, Format: time.RFC3339Nano}
}
// TimeUnix adds a time.Time field that outputs as a unix epoch (unsigned integer)
func TimeUnix(name string, value time.Time) FieldTimeUnix {
return FieldTimeUnix{Name: name, Value: value}
}
// TimeUnixNano adds a time.Time field that outputs as a unix epoch with nanosecond precision (unsigned integer)
func TimeUnixNano(name string, value time.Time) FieldTimeUnixNano {
return FieldTimeUnixNano{Name: name, Value: value}
}
// Uint adds an unsigned integer field
func Uint(name string, value uint) FieldUint64 {
return FieldUint64{Name: name, Value: uint64(value)}
}
// Uint8 adds an 8-bit unsigned integer field
func Uint8(name string, value uint8) FieldUint64 {
return FieldUint64{Name: name, Value: uint64(value)}
}
// Uint16 adds a 16-bit unsigned integer field
func Uint16(name string, value uint16) FieldUint64 {
return FieldUint64{Name: name, Value: uint64(value)}
}
// Uint32 adds a 32-bit unsigned integer field
func Uint32(name string, value uint32) FieldUint64 {
return FieldUint64{Name: name, Value: uint64(value)}
}
// Uint64 adds a 64-bit unsigned integer field
func Uint64(name string, value uint64) FieldUint64 {
return FieldUint64{Name: name, Value: value}
}
// Bool
type FieldBool struct {
Name string
Value bool
}
func (f FieldBool) Field() Field {
if f.Value {
return Field{Name: f.Name, Value: "true"}
}
return Field{Name: f.Name, Value: "false"}
}
// Duration
type FieldDuration struct {
Name string
Value time.Duration
}
func (f FieldDuration) Field() Field {
return Field{Name: f.Name, Value: f.Value.String(), IsJSONString: true, IsJSONSafe: true}
}
// Error
type FieldError struct {
Name string
Value error
}
func (f FieldError) Field() Field {
if f.Value == nil {
return Field{Name: f.Name, Value: "null"}
}
return Field{Name: f.Name, Value: f.Value.Error(), IsJSONString: true}
}
// Float32
type FieldFloat32 struct {
Name string
Value float32
}
func (f FieldFloat32) Field() Field {
return Field{Name: f.Name, Value: strconv.FormatFloat(float64(f.Value), 'g', -1, 32)}
}
// Float64
type FieldFloat64 struct {
Name string
Value float64
}
func (f FieldFloat64) Field() Field {
return Field{Name: f.Name, Value: strconv.FormatFloat(f.Value, 'g', -1, 64)}
}
// Int, Int8, Int16, Int32, Int64
type FieldInt64 struct {
Name string
Value int64
}
func (f FieldInt64) Field() Field {
return Field{Name: f.Name, Value: strconv.FormatInt(f.Value, 10)}
}
// String
type FieldString struct {
Name string
Value string
}
func (f FieldString) Field() Field {
return Field{Name: f.Name, Value: f.Value, IsJSONString: true, IsJSONSafe: false}
}
// Time
type FieldTimeFormat struct {
Name string
Value time.Time
Format string
}
func (f FieldTimeFormat) Field() Field {
return Field{Name: f.Name, Value: f.Value.Format(f.Format), IsJSONString: true, IsJSONSafe: true}
}
type FieldTimeUnix struct {
Name string
Value time.Time
}
func (f FieldTimeUnix) Field() Field {
return Field{Name: f.Name, Value: strconv.FormatInt(f.Value.Unix(), 10)}
}
type FieldTimeUnixNano struct {
Name string
Value time.Time
}
func (f FieldTimeUnixNano) Field() Field {
return Field{Name: f.Name, Value: strconv.FormatInt(f.Value.UnixNano(), 10)}
}
// Uint, Uint8, Uit16, Uint32, Uint64, Byte
type FieldUint64 struct {
Name string
Value uint64
}
func (f FieldUint64) Field() Field {
return Field{Name: f.Name, Value: strconv.FormatUint(f.Value, 10)}
}