If LevelFieldName is empty don't log level (#313)

This commit is contained in:
Tabitha 2021-05-05 14:40:45 +02:00 committed by GitHub
parent 0f923d7926
commit 19c98f6d3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

2
log.go
View File

@ -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 {

View File

@ -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().