Fix console write with missing level field
This commit is contained in:
parent
2a07580c27
commit
33f552ec3d
|
@ -328,7 +328,11 @@ func consoleDefaultFormatLevel(noColor bool) Formatter {
|
||||||
l = colorize("???", colorBold, noColor)
|
l = colorize("???", colorBold, noColor)
|
||||||
}
|
}
|
||||||
} else {
|
} 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
|
return l
|
||||||
}
|
}
|
||||||
|
|
|
@ -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}
|
||||||
|
|
Loading…
Reference in New Issue