Fix nil pointer exception on Discard when called with nil logger
Fixes #108
This commit is contained in:
parent
338f9bc140
commit
20ad1708e7
3
event.go
3
event.go
|
@ -92,6 +92,9 @@ func (e *Event) Enabled() bool {
|
||||||
|
|
||||||
// Discard disables the event so Msg(f) won't print it.
|
// Discard disables the event so Msg(f) won't print it.
|
||||||
func (e *Event) Discard() *Event {
|
func (e *Event) Discard() *Event {
|
||||||
|
if e == nil {
|
||||||
|
return e
|
||||||
|
}
|
||||||
e.level = Disabled
|
e.level = Disabled
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
15
log_test.go
15
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 {
|
type levelWriter struct {
|
||||||
ops []struct {
|
ops []struct {
|
||||||
l Level
|
l Level
|
||||||
|
|
Loading…
Reference in New Issue