diff --git a/event.go b/event.go index eec1286..eda2853 100644 --- a/event.go +++ b/event.go @@ -92,6 +92,9 @@ func (e *Event) Enabled() bool { // Discard disables the event so Msg(f) won't print it. func (e *Event) Discard() *Event { + if e == nil { + return e + } e.level = Disabled return nil } diff --git a/log_test.go b/log_test.go index 32cf52f..a4b66e4 100644 --- a/log_test.go +++ b/log_test.go @@ -403,6 +403,21 @@ func TestSampling(t *testing.T) { } } +func TestDiscard(t *testing.T) { + out := &bytes.Buffer{} + log := New(out) + log.Log().Discard().Str("a", "b").Msgf("one %s %.1f %d %v", "two", 3.4, 5, errors.New("six")) + if got, want := decodeIfBinaryToString(out.Bytes()), ""; got != want { + t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want) + } + + // Double call + log.Log().Discard().Discard().Str("a", "b").Msgf("one %s %.1f %d %v", "two", 3.4, 5, errors.New("six")) + if got, want := decodeIfBinaryToString(out.Bytes()), ""; got != want { + t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want) + } +} + type levelWriter struct { ops []struct { l Level