From 19c98f6d3ed5516199f94fa48b6eade7ea412961 Mon Sep 17 00:00:00 2001 From: Tabitha Date: Wed, 5 May 2021 14:40:45 +0200 Subject: [PATCH] If LevelFieldName is empty don't log level (#313) --- log.go | 2 +- log_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/log.go b/log.go index 2da3e68..4779e8c 100644 --- a/log.go +++ b/log.go @@ -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 { diff --git a/log_test.go b/log_test.go index 7899c78..82bcf8b 100644 --- a/log_test.go +++ b/log_test.go @@ -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().