@@ -25,6 +25,7 @@ import (
25
25
const timeRE = `\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}(Z|[+-]\d{2}:\d{2})`
26
26
27
27
func TestLogTextHandler (t * testing.T ) {
28
+ ctx := context .Background ()
28
29
var buf bytes.Buffer
29
30
30
31
l := New (NewTextHandler (& buf , nil ))
@@ -51,10 +52,10 @@ func TestLogTextHandler(t *testing.T) {
51
52
l .Error ("bad" , "a" , 1 )
52
53
check (`level=ERROR msg=bad a=1` )
53
54
54
- l .Log (nil , LevelWarn + 1 , "w" , Int ("a" , 1 ), String ("b" , "two" ))
55
+ l .Log (ctx , LevelWarn + 1 , "w" , Int ("a" , 1 ), String ("b" , "two" ))
55
56
check (`level=WARN\+1 msg=w a=1 b=two` )
56
57
57
- l .LogAttrs (nil , LevelInfo + 1 , "a b c" , Int ("a" , 1 ), String ("b" , "two" ))
58
+ l .LogAttrs (ctx , LevelInfo + 1 , "a b c" , Int ("a" , 1 ), String ("b" , "two" ))
58
59
check (`level=INFO\+1 msg="a b c" a=1 b=two` )
59
60
60
61
l .Info ("info" , "a" , []Attr {Int ("i" , 1 )})
@@ -156,6 +157,7 @@ func TestAttrs(t *testing.T) {
156
157
}
157
158
158
159
func TestCallDepth (t * testing.T ) {
160
+ ctx := context .Background ()
159
161
h := & captureHandler {}
160
162
var startLine int
161
163
@@ -181,9 +183,9 @@ func TestCallDepth(t *testing.T) {
181
183
startLine = f .Line + 4
182
184
// Do not change the number of lines between here and the call to check(0).
183
185
184
- logger .Log (nil , LevelInfo , "" )
186
+ logger .Log (ctx , LevelInfo , "" )
185
187
check (0 )
186
- logger .LogAttrs (nil , LevelInfo , "" )
188
+ logger .LogAttrs (ctx , LevelInfo , "" )
187
189
check (1 )
188
190
logger .Debug ("" )
189
191
check (2 )
@@ -201,13 +203,14 @@ func TestCallDepth(t *testing.T) {
201
203
check (8 )
202
204
Error ("" )
203
205
check (9 )
204
- Log (nil , LevelInfo , "" )
206
+ Log (ctx , LevelInfo , "" )
205
207
check (10 )
206
- LogAttrs (nil , LevelInfo , "" )
208
+ LogAttrs (ctx , LevelInfo , "" )
207
209
check (11 )
208
210
}
209
211
210
212
func TestAlloc (t * testing.T ) {
213
+ ctx := context .Background ()
211
214
dl := New (discardHandler {})
212
215
defer SetDefault (Default ()) // restore
213
216
SetDefault (dl )
@@ -222,7 +225,7 @@ func TestAlloc(t *testing.T) {
222
225
wantAllocs (t , 0 , func () { dl .Info ("hello" ) })
223
226
})
224
227
t .Run ("logger.Log" , func (t * testing.T ) {
225
- wantAllocs (t , 0 , func () { dl .Log (nil , LevelDebug , "hello" ) })
228
+ wantAllocs (t , 0 , func () { dl .Log (ctx , LevelDebug , "hello" ) })
226
229
})
227
230
t .Run ("2 pairs" , func (t * testing.T ) {
228
231
s := "abc"
@@ -239,7 +242,7 @@ func TestAlloc(t *testing.T) {
239
242
s := "abc"
240
243
i := 2000
241
244
wantAllocs (t , 2 , func () {
242
- l .Log (nil , LevelInfo , "hello" ,
245
+ l .Log (ctx , LevelInfo , "hello" ,
243
246
"n" , i ,
244
247
"s" , s ,
245
248
)
@@ -250,8 +253,8 @@ func TestAlloc(t *testing.T) {
250
253
s := "abc"
251
254
i := 2000
252
255
wantAllocs (t , 0 , func () {
253
- if l .Enabled (nil , LevelInfo ) {
254
- l .Log (nil , LevelInfo , "hello" ,
256
+ if l .Enabled (ctx , LevelInfo ) {
257
+ l .Log (ctx , LevelInfo , "hello" ,
255
258
"n" , i ,
256
259
"s" , s ,
257
260
)
@@ -273,30 +276,30 @@ func TestAlloc(t *testing.T) {
273
276
wantAllocs (t , 0 , func () { dl .Info ("" , "error" , io .EOF ) })
274
277
})
275
278
t .Run ("attrs1" , func (t * testing.T ) {
276
- wantAllocs (t , 0 , func () { dl .LogAttrs (nil , LevelInfo , "" , Int ("a" , 1 )) })
277
- wantAllocs (t , 0 , func () { dl .LogAttrs (nil , LevelInfo , "" , Any ("error" , io .EOF )) })
279
+ wantAllocs (t , 0 , func () { dl .LogAttrs (ctx , LevelInfo , "" , Int ("a" , 1 )) })
280
+ wantAllocs (t , 0 , func () { dl .LogAttrs (ctx , LevelInfo , "" , Any ("error" , io .EOF )) })
278
281
})
279
282
t .Run ("attrs3" , func (t * testing.T ) {
280
283
wantAllocs (t , 0 , func () {
281
- dl .LogAttrs (nil , LevelInfo , "hello" , Int ("a" , 1 ), String ("b" , "two" ), Duration ("c" , time .Second ))
284
+ dl .LogAttrs (ctx , LevelInfo , "hello" , Int ("a" , 1 ), String ("b" , "two" ), Duration ("c" , time .Second ))
282
285
})
283
286
})
284
287
t .Run ("attrs3 disabled" , func (t * testing.T ) {
285
288
logger := New (discardHandler {disabled : true })
286
289
wantAllocs (t , 0 , func () {
287
- logger .LogAttrs (nil , LevelInfo , "hello" , Int ("a" , 1 ), String ("b" , "two" ), Duration ("c" , time .Second ))
290
+ logger .LogAttrs (ctx , LevelInfo , "hello" , Int ("a" , 1 ), String ("b" , "two" ), Duration ("c" , time .Second ))
288
291
})
289
292
})
290
293
t .Run ("attrs6" , func (t * testing.T ) {
291
294
wantAllocs (t , 1 , func () {
292
- dl .LogAttrs (nil , LevelInfo , "hello" ,
295
+ dl .LogAttrs (ctx , LevelInfo , "hello" ,
293
296
Int ("a" , 1 ), String ("b" , "two" ), Duration ("c" , time .Second ),
294
297
Int ("d" , 1 ), String ("e" , "two" ), Duration ("f" , time .Second ))
295
298
})
296
299
})
297
300
t .Run ("attrs9" , func (t * testing.T ) {
298
301
wantAllocs (t , 1 , func () {
299
- dl .LogAttrs (nil , LevelInfo , "hello" ,
302
+ dl .LogAttrs (ctx , LevelInfo , "hello" ,
300
303
Int ("a" , 1 ), String ("b" , "two" ), Duration ("c" , time .Second ),
301
304
Int ("d" , 1 ), String ("e" , "two" ), Duration ("f" , time .Second ),
302
305
Int ("d" , 1 ), String ("e" , "two" ), Duration ("f" , time .Second ))
@@ -511,27 +514,27 @@ func BenchmarkNopLog(b *testing.B) {
511
514
b .Run ("no attrs" , func (b * testing.B ) {
512
515
b .ReportAllocs ()
513
516
for i := 0 ; i < b .N ; i ++ {
514
- l .LogAttrs (nil , LevelInfo , "msg" )
517
+ l .LogAttrs (ctx , LevelInfo , "msg" )
515
518
}
516
519
})
517
520
b .Run ("attrs" , func (b * testing.B ) {
518
521
b .ReportAllocs ()
519
522
for i := 0 ; i < b .N ; i ++ {
520
- l .LogAttrs (nil , LevelInfo , "msg" , Int ("a" , 1 ), String ("b" , "two" ), Bool ("c" , true ))
523
+ l .LogAttrs (ctx , LevelInfo , "msg" , Int ("a" , 1 ), String ("b" , "two" ), Bool ("c" , true ))
521
524
}
522
525
})
523
526
b .Run ("attrs-parallel" , func (b * testing.B ) {
524
527
b .ReportAllocs ()
525
528
b .RunParallel (func (pb * testing.PB ) {
526
529
for pb .Next () {
527
- l .LogAttrs (nil , LevelInfo , "msg" , Int ("a" , 1 ), String ("b" , "two" ), Bool ("c" , true ))
530
+ l .LogAttrs (ctx , LevelInfo , "msg" , Int ("a" , 1 ), String ("b" , "two" ), Bool ("c" , true ))
528
531
}
529
532
})
530
533
})
531
534
b .Run ("keys-values" , func (b * testing.B ) {
532
535
b .ReportAllocs ()
533
536
for i := 0 ; i < b .N ; i ++ {
534
- l .Log (nil , LevelInfo , "msg" , "a" , 1 , "b" , "two" , "c" , true )
537
+ l .Log (ctx , LevelInfo , "msg" , "a" , 1 , "b" , "two" , "c" , true )
535
538
}
536
539
})
537
540
b .Run ("WithContext" , func (b * testing.B ) {
0 commit comments