Browse Source

Merge pull request #12 from tendermint/feature/helper-log-func

[log] add NewFilterByLevel helper func
pull/1842/head
Anton Kaliaev 8 years ago
committed by GitHub
parent
commit
9e1852c08d
2 changed files with 31 additions and 0 deletions
  1. +18
    -0
      log/filter.go
  2. +13
    -0
      log/filter_test.go

+ 18
- 0
log/filter.go View File

@ -1,5 +1,7 @@
package log package log
import "fmt"
// NewFilter wraps next and implements filtering. See the commentary on the // NewFilter wraps next and implements filtering. See the commentary on the
// Option functions for a detailed description of how to configure levels. If // Option functions for a detailed description of how to configure levels. If
// no options are provided, all leveled log events created with Debug, Info or // no options are provided, all leveled log events created with Debug, Info or
@ -14,6 +16,22 @@ func NewFilter(next Logger, options ...Option) Logger {
return l return l
} }
// NewFilterByLevel wraps next and implements filtering based on a given level.
func NewFilterByLevel(next Logger, lvl string) Logger {
var option Option
switch lvl {
case "info":
option = AllowInfo()
case "debug":
option = AllowDebug()
case "error":
option = AllowError()
default:
panic(fmt.Sprintf("Expected either \"info\", \"debug\" or \"error\" log level, given %v", lvl))
}
return NewFilter(next, option)
}
type filter struct { type filter struct {
next Logger next Logger
allowed level allowed level


+ 13
- 0
log/filter_test.go View File

@ -6,6 +6,7 @@ import (
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tmlibs/log" "github.com/tendermint/tmlibs/log"
) )
@ -107,3 +108,15 @@ func TestLevelContext(t *testing.T) {
t.Errorf("\nwant '%s'\nhave '%s'", want, have) t.Errorf("\nwant '%s'\nhave '%s'", want, have)
} }
} }
func TestNewFilterByLevel(t *testing.T) {
assert := assert.New(t)
var logger log.Logger
logger = log.NewNopLogger()
assert.NotPanics(func() {
logger = log.NewFilterByLevel(logger, "info")
})
assert.Panics(func() {
logger = log.NewFilterByLevel(logger, "smth")
})
}

Loading…
Cancel
Save