Fix console write with missing level field

This commit is contained in:
Olivier Poitrey 2019-04-25 12:12:24 -07:00
parent 2a07580c27
commit 33f552ec3d
2 changed files with 21 additions and 1 deletions

View File

@ -327,9 +327,13 @@ func consoleDefaultFormatLevel(noColor bool) Formatter {
default: default:
l = colorize("???", colorBold, noColor) l = colorize("???", colorBold, noColor)
} }
} else {
if i == nil {
l = colorize("???", colorBold, noColor)
} else { } else {
l = strings.ToUpper(fmt.Sprintf("%s", i))[0:3] l = strings.ToUpper(fmt.Sprintf("%s", i))[0:3]
} }
}
return l return l
} }
} }

View File

@ -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 := "<nil> ??? 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) { t.Run("Write colorized fields", func(t *testing.T) {
buf := &bytes.Buffer{} buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: false} w := zerolog.ConsoleWriter{Out: buf, NoColor: false}