diff --git a/console.go b/console.go index b9f4f4b..2c28754 100644 --- a/console.go +++ b/console.go @@ -328,7 +328,11 @@ func consoleDefaultFormatLevel(noColor bool) Formatter { l = colorize("???", colorBold, noColor) } } else { - l = strings.ToUpper(fmt.Sprintf("%s", i))[0:3] + if i == nil { + l = colorize("???", colorBold, noColor) + } else { + l = strings.ToUpper(fmt.Sprintf("%s", i))[0:3] + } } return l } diff --git a/console_test.go b/console_test.go index f745545..2cc335a 100644 --- a/console_test.go +++ b/console_test.go @@ -159,6 +159,22 @@ func TestConsoleWriter(t *testing.T) { } }) + t.Run("No level field", func(t *testing.T) { + buf := &bytes.Buffer{} + w := zerolog.ConsoleWriter{Out: buf, NoColor: true} + + _, err := w.Write([]byte(`{"message": "Foobar", "foo": "bar"}`)) + if err != nil { + t.Errorf("Unexpected error when writing output: %s", err) + } + + expectedOutput := " ??? Foobar foo=bar\n" + actualOutput := buf.String() + if actualOutput != expectedOutput { + t.Errorf("Unexpected output %q, want: %q", actualOutput, expectedOutput) + } + }) + t.Run("Write colorized fields", func(t *testing.T) { buf := &bytes.Buffer{} w := zerolog.ConsoleWriter{Out: buf, NoColor: false}