Skip to content

Commit

Permalink
Added IsInfo() to Logger interface
Browse files Browse the repository at this point in the history
There are still times when it’s useful to shut off a block of expensive logging code if the logger is disabled.
  • Loading branch information
ansel1 committed Oct 9, 2019
1 parent 2a240b1 commit ad13956
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,11 @@ func (l *Core) IsDebug() bool {
return l.IsEnabled(DebugLevel)
}

// IsDebug returns true if INF level is enabled
func (l *Core) IsInfo() bool {
return l.IsEnabled(InfoLevel)
}

// With returns a new Logger with some context baked in. All entries
// logged with the new logger will include this context.
//
Expand Down
28 changes: 28 additions & 0 deletions core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,31 @@ func TestSweetenFields(t *testing.T) {
fields = c.sweetenFields([]interface{}{err})
assert.Equal(t, []zap.Field{zap.NamedError("error", err)}, fields)
}

func TestCore_IsDebug(t *testing.T) {
f := NewFactory()
f.SetDefaultLevel(InfoLevel)

l := f.NewLogger("asdf")
assert.False(t, l.IsDebug())
f.SetDefaultLevel(DebugLevel)
assert.True(t, l.IsDebug())
f.SetDefaultLevel(ErrorLevel)
assert.False(t, l.IsDebug())
f.SetLevel("asdf", DebugLevel)
assert.True(t, l.IsDebug())
}

func TestCore_IsInfo(t *testing.T) {
f := NewFactory()
f.SetDefaultLevel(ErrorLevel)

l := f.NewLogger("asdf")
assert.False(t, l.IsInfo())
f.SetDefaultLevel(InfoLevel)
assert.True(t, l.IsInfo())
f.SetDefaultLevel(ErrorLevel)
assert.False(t, l.IsInfo())
f.SetLevel("asdf", DebugLevel)
assert.True(t, l.IsInfo())
}
1 change: 1 addition & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type (
Error(msg string, args ...interface{})

IsDebug() bool
IsInfo() bool

// With creates a new Logger with some context already attached. All
// entries logged with the child logger will include this context.
Expand Down

0 comments on commit ad13956

Please sign in to comment.