Skip to content

Commit

Permalink
Add the LevelEnablerFunc and test case (#208)
Browse files Browse the repository at this point in the history
* add the levelenablerfunc for #201

* add the test for levelenablerfunc #205

* remove the comment in test and change code into the buf.Line()
  • Loading branch information
arthurkiller authored and akshayjshah committed Dec 12, 2016
1 parent 35cd796 commit 05dadc4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions level.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ const (
FatalLevel
)

// LevelEnablerFunc is a convenient way to implement LevelEnabler around an
// anonymous function. It is also a valid Option to pass to a logger.
type LevelEnablerFunc func(Level) bool

// This allows an LevelEnablerFunc to be used as an option.
func (f LevelEnablerFunc) apply(m *Meta) { m.LevelEnabler = f }

// Enabled calls the wrapped function.
func (f LevelEnablerFunc) Enabled(lvl Level) bool { return f(lvl) }

// String returns a lower-case ASCII representation of the log level.
func (l Level) String() string {
switch l {
Expand Down
11 changes: 11 additions & 0 deletions level_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@ import (
"github.com/stretchr/testify/assert"
)

func TestLevelEnablerFunc(t *testing.T) {
opts := []Option{Fields(Int("foo", 42)), LevelEnablerFunc(func(l Level) bool { return l == DebugLevel })}
withJSONLogger(t, opts, func(log Logger, buf *testBuffer) {
log.Debug("@debug", Int("logger", 0))
log.Info("@info", Int("logger", 0))
assert.Equal(t, []string{
`{"level":"debug","msg":"@debug","foo":42,"logger":0}`,
}, buf.Lines())
})
}

func TestLevelString(t *testing.T) {
tests := map[Level]string{
DebugLevel: "debug",
Expand Down

0 comments on commit 05dadc4

Please sign in to comment.