@@ -320,6 +320,85 @@ func TestConsoleWriter(t *testing.T) {
320
320
t .Errorf ("Unexpected output %q, want: %q" , actualOutput , expectedOutput )
321
321
}
322
322
})
323
+
324
+ t .Run ("With an extra 'level' field" , func (t * testing.T ) {
325
+ t .Run ("malformed string" , func (t * testing.T ) {
326
+ cases := []struct {
327
+ field string
328
+ output string
329
+ }{
330
+ {"" , "<nil> ??? Hello World foo=bar\n " },
331
+ {"-" , "<nil> - Hello World foo=bar\n " },
332
+ {"1" , "<nil> " + zerolog .FormattedLevels [1 ] + " Hello World foo=bar\n " },
333
+ {"a" , "<nil> A Hello World foo=bar\n " },
334
+ {"12" , "<nil> 12 Hello World foo=bar\n " },
335
+ {"a2" , "<nil> A2 Hello World foo=bar\n " },
336
+ {"2a" , "<nil> 2A Hello World foo=bar\n " },
337
+ {"ab" , "<nil> AB Hello World foo=bar\n " },
338
+ {"12a" , "<nil> 12A Hello World foo=bar\n " },
339
+ {"a12" , "<nil> A12 Hello World foo=bar\n " },
340
+ {"abc" , "<nil> ABC Hello World foo=bar\n " },
341
+ {"123" , "<nil> 123 Hello World foo=bar\n " },
342
+ {"abcd" , "<nil> ABC Hello World foo=bar\n " },
343
+ {"1234" , "<nil> 123 Hello World foo=bar\n " },
344
+ {"123d" , "<nil> 123 Hello World foo=bar\n " },
345
+ {"01" , "<nil> " + zerolog .FormattedLevels [1 ] + " Hello World foo=bar\n " },
346
+ {"001" , "<nil> " + zerolog .FormattedLevels [1 ] + " Hello World foo=bar\n " },
347
+ {"0001" , "<nil> " + zerolog .FormattedLevels [1 ] + " Hello World foo=bar\n " },
348
+ }
349
+ for i , c := range cases {
350
+ c := c
351
+ t .Run (fmt .Sprintf ("case %d" , i ), func (t * testing.T ) {
352
+ buf := & bytes.Buffer {}
353
+ out := zerolog .NewConsoleWriter ()
354
+ out .NoColor = true
355
+ out .Out = buf
356
+ log := zerolog .New (out )
357
+
358
+ log .Debug ().Str ("level" , c .field ).Str ("foo" , "bar" ).Msg ("Hello World" )
359
+
360
+ actualOutput := buf .String ()
361
+ if actualOutput != c .output {
362
+ t .Errorf ("Unexpected output %q, want: %q" , actualOutput , c .output )
363
+ }
364
+ })
365
+ }
366
+ })
367
+
368
+ t .Run ("weird value" , func (t * testing.T ) {
369
+ cases := []struct {
370
+ field interface {}
371
+ output string
372
+ }{
373
+ {0 , "<nil> 0 Hello World foo=bar\n " },
374
+ {1 , "<nil> 1 Hello World foo=bar\n " },
375
+ {- 1 , "<nil> -1 Hello World foo=bar\n " },
376
+ {- 3 , "<nil> -3 Hello World foo=bar\n " },
377
+ {- 32 , "<nil> -32 Hello World foo=bar\n " },
378
+ {- 321 , "<nil> -32 Hello World foo=bar\n " },
379
+ {12 , "<nil> 12 Hello World foo=bar\n " },
380
+ {123 , "<nil> 123 Hello World foo=bar\n " },
381
+ {1234 , "<nil> 123 Hello World foo=bar\n " },
382
+ }
383
+ for i , c := range cases {
384
+ c := c
385
+ t .Run (fmt .Sprintf ("case %d" , i ), func (t * testing.T ) {
386
+ buf := & bytes.Buffer {}
387
+ out := zerolog .NewConsoleWriter ()
388
+ out .NoColor = true
389
+ out .Out = buf
390
+ log := zerolog .New (out )
391
+
392
+ log .Debug ().Interface ("level" , c .field ).Str ("foo" , "bar" ).Msg ("Hello World" )
393
+
394
+ actualOutput := buf .String ()
395
+ if actualOutput != c .output {
396
+ t .Errorf ("Unexpected output %q, want: %q" , actualOutput , c .output )
397
+ }
398
+ })
399
+ }
400
+ })
401
+ })
323
402
}
324
403
325
404
func TestConsoleWriterConfiguration (t * testing.T ) {
0 commit comments