Browse Source

[log] NewFilterByLevel returns an error

pull/1842/head
Anton Kaliaev 8 years ago
parent
commit
dd3e433d32
No known key found for this signature in database GPG Key ID: 7B6881D965918214
2 changed files with 10 additions and 11 deletions
  1. +4
    -3
      log/filter.go
  2. +6
    -8
      log/filter_test.go

+ 4
- 3
log/filter.go View File

@ -17,7 +17,8 @@ func NewFilter(next Logger, options ...Option) Logger {
}
// NewFilterByLevel wraps next and implements filtering based on a given level.
func NewFilterByLevel(next Logger, lvl string) Logger {
// Error is returned if level is not info, error or debug.
func NewFilterByLevel(next Logger, lvl string) (Logger, error) {
var option Option
switch lvl {
case "info":
@ -27,9 +28,9 @@ func NewFilterByLevel(next Logger, lvl string) Logger {
case "error":
option = AllowError()
default:
panic(fmt.Sprintf("Expected either \"info\", \"debug\" or \"error\" log level, given %v", lvl))
return nil, fmt.Errorf("Expected either \"info\", \"debug\" or \"error\" log level, given %v", lvl)
}
return NewFilter(next, option)
return NewFilter(next, option), nil
}
type filter struct {


+ 6
- 8
log/filter_test.go View File

@ -6,7 +6,6 @@ import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/tendermint/tmlibs/log"
)
@ -110,13 +109,12 @@ func TestLevelContext(t *testing.T) {
}
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")
})
if _, err := log.NewFilterByLevel(logger, "info"); err != nil {
t.Fatal(err)
}
if _, err := log.NewFilterByLevel(logger, "other"); err == nil {
t.Fatal(err)
}
}

Loading…
Cancel
Save