Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ func (l Logger) With() Context {
// Caution: This method is not concurrency safe.
// Use the With method to create a child logger before modifying the context from concurrent goroutines.
func (l *Logger) UpdateContext(update func(c Context) Context) {
if l == disabledLogger {
if l.disabled() {
return
}
if cap(l.context) == 0 {
Expand Down Expand Up @@ -508,9 +508,14 @@ func (l *Logger) scratchEvent() *Event {
return newEvent(LevelWriterAdapter{io.Discard}, DebugLevel, l.stack, l.ctx, l.hooks)
}

// disabled returns true if the logger is a disabled or nop logger.
func (l *Logger) disabled() bool {
return l.w == nil || l.level == Disabled
}

// should returns true if the log event should be logged.
func (l *Logger) should(lvl Level) bool {
if l.w == nil {
if l.disabled() {
return false
}
if lvl < l.level || lvl < GlobalLevel() {
Expand Down
33 changes: 33 additions & 0 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,39 @@ func TestUpdateContextOnDisabledLogger(t *testing.T) {
}
}

func TestUpdateContextOnNopLogger(t *testing.T) {
// Nop() creates a new disabled logger that is a different pointer than
// the package-level disabledLogger. UpdateContext should still treat it
// as disabled and skip the update function. See issue #643.
log := Nop()

called := false
log.UpdateContext(func(c Context) Context {
called = true
return c.Str("foo", "bar")
})

if called {
t.Error("UpdateContext should not call update func on a Nop logger")
}
}

func TestUpdateContextOnZeroValueLogger(t *testing.T) {
// A zero-value Logger has a nil writer and should be treated as
// disabled by UpdateContext. See issue #643.
var log Logger

called := false
log.UpdateContext(func(c Context) Context {
called = true
return c.Str("foo", "bar")
})

if called {
t.Error("UpdateContext should not call update func on a zero-value logger")
}
}

func TestLevel_String(t *testing.T) {
tests := []struct {
name string
Expand Down