Skip to content

Commit

Permalink
If LevelFieldName is empty don't log level (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
keyneston authored May 5, 2021
1 parent 0f923d7 commit 19c98f6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion log.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ func (l *Logger) newEvent(level Level, done func(string)) *Event {
e := newEvent(l.w, level)
e.done = done
e.ch = l.hooks
if level != NoLevel {
if level != NoLevel && LevelFieldName != "" {
e.Str(LevelFieldName, LevelFieldMarshalFunc(level))
}
if l.context != nil && len(l.context) > 1 {
Expand Down
18 changes: 18 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,24 @@ func TestInfo(t *testing.T) {
})
}

func TestEmptyLevelFieldName(t *testing.T) {
fieldName := LevelFieldName
LevelFieldName = ""

t.Run("empty setting", func(t *testing.T) {
out := &bytes.Buffer{}
log := New(out)
log.Info().
Str("foo", "bar").
Int("n", 123).
Msg("")
if got, want := decodeIfBinaryToString(out.Bytes()), `{"foo":"bar","n":123}`+"\n"; got != want {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
}
})
LevelFieldName = fieldName
}

func TestWith(t *testing.T) {
out := &bytes.Buffer{}
ctx := New(out).With().
Expand Down

0 comments on commit 19c98f6

Please sign in to comment.