-
Notifications
You must be signed in to change notification settings - Fork 11
/
format_test.go
292 lines (282 loc) · 7.7 KB
/
format_test.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
package log
import (
"bytes"
"context"
"errors"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestFormat(t *testing.T) {
now, epoc := timeNow, epoch
timeNow = func() time.Time { return time.Date(2022, time.January, 9, 20, 29, 45, 123, time.UTC) }
epoch = timeNow()
defer func() { timeNow = now; epoch = epoc }()
keyVals := []KV{
{"string", "val"},
{"stringWithQuotes", `example "val"`},
{"int", 1},
{"int32", int32(2)},
{"int64", int64(3)},
{"uint", uint(4)},
{"uint32", uint32(5)},
{"uint64", uint64(6)},
{"float32", float32(7)},
{"float64", float64(8.1)},
{"bool", true},
{"nil", nil},
{"dur", 123 * time.Millisecond},
{"sliceString", []string{"a", "b", "c"}},
{"sliceStringWithQuotes", []string{"example \"a\""}},
{"sliceInt", []int{1, 1}},
{"sliceInt32", []int32{2, 2}},
{"sliceInt64", []int64{3, 3}},
{"sliceUint", []uint{4, 4}},
{"sliceUint32", []uint32{5, 5}},
{"sliceUint64", []uint64{6, 6}},
{"sliceFloat32", []float32{7, 7}},
{"sliceFloat64", []float64{8.1, 8.1}},
{"sliceBool", []bool{true, false, true}},
{"sliceNil", []interface{}{nil, nil, nil}},
{"sliceMix", []interface{}{"a", 1, true, nil}},
}
formattedKeyVals := "string=val " +
`stringWithQuotes="example \"val\"" ` +
"int=1 " +
"int32=2 " +
"int64=3 " +
"uint=4 " +
"uint32=5 " +
"uint64=6 " +
"float32=7 " +
"float64=8.1 " +
"bool=true " +
"nil=<nil> " +
"dur=123ms " +
`sliceString=[a b c] ` +
`sliceStringWithQuotes=[example "a"] ` +
"sliceInt=[1 1] " +
"sliceInt32=[2 2] " +
"sliceInt64=[3 3] " +
"sliceUint=[4 4] " +
"sliceUint32=[5 5] " +
"sliceUint64=[6 6] " +
"sliceFloat32=[7 7] " +
"sliceFloat64=[8.1 8.1] " +
"sliceBool=[true false true] " +
"sliceNil=[<nil> <nil> <nil>] " +
"sliceMix=[a 1 true <nil>]"
coloredKeyVals := func(col string) string {
return col + "string\033[0m=val " +
col + "stringWithQuotes\033[0m=example \"val\" " +
col + "int\033[0m=1 " +
col + "int32\033[0m=2 " +
col + "int64\033[0m=3 " +
col + "uint\033[0m=4 " +
col + "uint32\033[0m=5 " +
col + "uint64\033[0m=6 " +
col + "float32\033[0m=7 " +
col + "float64\033[0m=8.1 " +
col + "bool\033[0m=true " +
col + "nil\033[0m=<nil> " +
col + "dur\033[0m=123ms " +
col + "sliceString\033[0m=[a b c] " +
col + "sliceStringWithQuotes\033[0m=[example \"a\"] " +
col + "sliceInt\033[0m=[1 1] " +
col + "sliceInt32\033[0m=[2 2] " +
col + "sliceInt64\033[0m=[3 3] " +
col + "sliceUint\033[0m=[4 4] " +
col + "sliceUint32\033[0m=[5 5] " +
col + "sliceUint64\033[0m=[6 6] " +
col + "sliceFloat32\033[0m=[7 7] " +
col + "sliceFloat64\033[0m=[8.1 8.1] " +
col + "sliceBool\033[0m=[true false true] " +
col + "sliceNil\033[0m=[<nil> <nil> <nil>] " +
col + "sliceMix\033[0m=[a 1 true <nil>]"
}
jsonKeyVals := `"string":"val",` +
`"stringWithQuotes":"example \"val\"",` +
`"int":1,` +
`"int32":2,` +
`"int64":3,` +
`"uint":4,` +
`"uint32":5,` +
`"uint64":6,` +
`"float32":7,` +
`"float64":8.1,` +
`"bool":true,` +
`"nil":null,` +
`"dur":"123ms",` +
`"sliceString":["a","b","c"],` +
`"sliceStringWithQuotes":["example \"a\""],` +
`"sliceInt":[1,1],` +
`"sliceInt32":[2,2],` +
`"sliceInt64":[3,3],` +
`"sliceUint":[4,4],` +
`"sliceUint32":[5,5],` +
`"sliceUint64":[6,6],` +
`"sliceFloat32":[7,7],` +
`"sliceFloat64":[8.1,8.1],` +
`"sliceBool":[true,false,true],` +
`"sliceNil":[null,null,null],` +
`"sliceMix":["a",1,true,null]`
cases := []struct {
name string
logfn func(ctx context.Context, keyvals ...Fielder)
format FormatFunc
keyVals []KV
want string
}{
{
name: "default debug",
logfn: Debug,
format: FormatText,
keyVals: keyVals,
want: "time=2022-01-09T20:29:45Z level=debug " + formattedKeyVals + "\n",
},
{
name: "default info",
logfn: Info,
format: FormatText,
keyVals: keyVals,
want: "time=2022-01-09T20:29:45Z level=info " + formattedKeyVals + "\n",
},
{
name: "default print",
logfn: Print,
format: FormatText,
keyVals: keyVals,
want: "time=2022-01-09T20:29:45Z level=info " + formattedKeyVals + "\n",
},
{
name: "colored debug",
logfn: Debug,
format: FormatTerminal,
keyVals: keyVals,
want: "\033[37mDEBG\033[0m[0000] " + coloredKeyVals("\033[37m") + "\n",
},
{
name: "colored info",
logfn: Info,
format: FormatTerminal,
keyVals: keyVals,
want: "\033[34mINFO\033[0m[0000] " + coloredKeyVals("\033[34m") + "\n",
},
{
name: "colored print",
logfn: Print,
format: FormatTerminal,
keyVals: keyVals,
want: "\033[34mINFO\033[0m[0000] " + coloredKeyVals("\033[34m") + "\n",
},
{
name: "json debug",
logfn: Debug,
format: FormatJSON,
keyVals: keyVals,
want: `{"time":"2022-01-09T20:29:45Z","level":"debug",` + jsonKeyVals + "}\n",
},
{
name: "json info",
logfn: Info,
format: FormatJSON,
keyVals: keyVals,
want: `{"time":"2022-01-09T20:29:45Z","level":"info",` + jsonKeyVals + "}\n",
},
{
name: "json print",
logfn: Print,
format: FormatJSON,
keyVals: keyVals,
want: `{"time":"2022-01-09T20:29:45Z","level":"info",` + jsonKeyVals + "}\n",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
ctx := Context(context.Background(), WithOutput(&buf), WithFormat(tc.format), WithDebug())
tc.logfn(ctx, kvList(tc.keyVals))
assert.Equal(t, tc.want, buf.String())
})
}
errorCases := []struct {
name string
logfn func(ctx context.Context, err error, keyvals ...Fielder)
format FormatFunc
keyVals []KV
want string
}{
{
name: "default error",
logfn: Error,
format: FormatText,
keyVals: keyVals,
want: "time=2022-01-09T20:29:45Z level=error err=error " + formattedKeyVals + "\n",
},
{
name: "colored error",
logfn: Error,
format: FormatTerminal,
keyVals: keyVals,
want: "\033[1;31mERRO\033[0m[0000] " + "\033[1;31merr\033[0m=error " + coloredKeyVals("\033[1;31m") + "\n",
},
{
name: "json info",
logfn: Error,
format: FormatJSON,
keyVals: keyVals,
want: `{"time":"2022-01-09T20:29:45Z","level":"error","err":"error",` + jsonKeyVals + `}` + "\n",
},
}
for _, tc := range errorCases {
t.Run(tc.name, func(t *testing.T) {
var buf bytes.Buffer
ctx := Context(context.Background(), WithOutput(&buf), WithFormat(tc.format), WithDebug())
tc.logfn(ctx, errors.New("error"), kvList(tc.keyVals))
assert.Equal(t, tc.want, buf.String())
})
}
customisedCases := []struct {
name string
logfn func(ctx context.Context, keyvals ...Fielder)
format FormatFunc
keyVals []KV
want string
}{
{
name: "default debug",
logfn: Debug,
format: FormatText,
keyVals: keyVals,
want: "timestamp=2022-01-09T20:29:45.000000123Z severity=debug " + formattedKeyVals + "\n",
},
{
name: "json debug",
logfn: Debug,
format: FormatJSON,
keyVals: keyVals,
want: `{"timestamp":"2022-01-09T20:29:45.000000123Z","severity":"debug",` + jsonKeyVals + "}\n",
},
}
timestampKey := TimestampKey
timestampFormatLayout := TimestampFormatLayout
severityKey := SeverityKey
for _, tc := range customisedCases {
t.Run(tc.name, func(t *testing.T) {
{
TimestampKey = "timestamp"
TimestampFormatLayout = time.RFC3339Nano
SeverityKey = "severity"
}
defer func() {
TimestampKey = timestampKey
TimestampFormatLayout = timestampFormatLayout
SeverityKey = severityKey
}()
var buf bytes.Buffer
ctx := Context(context.Background(), WithOutput(&buf), WithFormat(tc.format), WithDebug())
tc.logfn(ctx, kvList(tc.keyVals))
assert.Equal(t, tc.want, buf.String())
})
}
}