Skip to content

Commit

Permalink
Fix console write with missing level field
Browse files Browse the repository at this point in the history
  • Loading branch information
rs committed Apr 25, 2019
1 parent 2a07580 commit 33f552e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
6 changes: 5 additions & 1 deletion console.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions console_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
buf := &bytes.Buffer{}
w := zerolog.ConsoleWriter{Out: buf, NoColor: false}
Expand Down

0 comments on commit 33f552e

Please sign in to comment.