Skip to content

Commit 19c98f6

Browse files
authored
If LevelFieldName is empty don't log level (#313)
1 parent 0f923d7 commit 19c98f6

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ func (l *Logger) newEvent(level Level, done func(string)) *Event {
424424
e := newEvent(l.w, level)
425425
e.done = done
426426
e.ch = l.hooks
427-
if level != NoLevel {
427+
if level != NoLevel && LevelFieldName != "" {
428428
e.Str(LevelFieldName, LevelFieldMarshalFunc(level))
429429
}
430430
if l.context != nil && len(l.context) > 1 {

log_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,24 @@ func TestInfo(t *testing.T) {
7777
})
7878
}
7979

80+
func TestEmptyLevelFieldName(t *testing.T) {
81+
fieldName := LevelFieldName
82+
LevelFieldName = ""
83+
84+
t.Run("empty setting", func(t *testing.T) {
85+
out := &bytes.Buffer{}
86+
log := New(out)
87+
log.Info().
88+
Str("foo", "bar").
89+
Int("n", 123).
90+
Msg("")
91+
if got, want := decodeIfBinaryToString(out.Bytes()), `{"foo":"bar","n":123}`+"\n"; got != want {
92+
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
93+
}
94+
})
95+
LevelFieldName = fieldName
96+
}
97+
8098
func TestWith(t *testing.T) {
8199
out := &bytes.Buffer{}
82100
ctx := New(out).With().

0 commit comments

Comments
 (0)