mirror of
https://github.com/fluencelabs/tendermint
synced 2025-06-17 15:11:21 +00:00
filter
This commit is contained in:
97
log/filter.go
Normal file
97
log/filter.go
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
package log
|
||||||
|
|
||||||
|
// NewFilter wraps next and implements filtering. See the commentary on the
|
||||||
|
// 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
|
||||||
|
// Error helper methods are squelched.
|
||||||
|
func NewFilter(next Logger, options ...Option) Logger {
|
||||||
|
l := &filter{
|
||||||
|
next: next,
|
||||||
|
}
|
||||||
|
for _, option := range options {
|
||||||
|
option(l)
|
||||||
|
}
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
type filter struct {
|
||||||
|
next Logger
|
||||||
|
allowed level
|
||||||
|
errNotAllowed error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *filter) Info(msg string, keyvals ...interface{}) error {
|
||||||
|
levelAllowed := l.allowed&levelInfo != 0
|
||||||
|
if !levelAllowed {
|
||||||
|
return l.errNotAllowed
|
||||||
|
}
|
||||||
|
return l.next.Info(msg, keyvals...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *filter) Debug(msg string, keyvals ...interface{}) error {
|
||||||
|
levelAllowed := l.allowed&levelDebug != 0
|
||||||
|
if !levelAllowed {
|
||||||
|
return l.errNotAllowed
|
||||||
|
}
|
||||||
|
return l.next.Debug(msg, keyvals...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *filter) Error(msg string, keyvals ...interface{}) error {
|
||||||
|
levelAllowed := l.allowed&levelError != 0
|
||||||
|
if !levelAllowed {
|
||||||
|
return l.errNotAllowed
|
||||||
|
}
|
||||||
|
return l.next.Error(msg, keyvals...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *filter) With(keyvals ...interface{}) Logger {
|
||||||
|
return l.next.With(keyvals...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Option sets a parameter for the filter.
|
||||||
|
type Option func(*filter)
|
||||||
|
|
||||||
|
// AllowAll is an alias for AllowDebug.
|
||||||
|
func AllowAll() Option {
|
||||||
|
return AllowDebug()
|
||||||
|
}
|
||||||
|
|
||||||
|
// AllowDebug allows error, warn, info and debug level log events to pass.
|
||||||
|
func AllowDebug() Option {
|
||||||
|
return allowed(levelError | levelInfo | levelDebug)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AllowInfo allows error, warn and info level log events to pass.
|
||||||
|
func AllowInfo() Option {
|
||||||
|
return allowed(levelError | levelInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AllowError allows only error level log events to pass.
|
||||||
|
func AllowError() Option {
|
||||||
|
return allowed(levelError)
|
||||||
|
}
|
||||||
|
|
||||||
|
// AllowNone allows no leveled log events to pass.
|
||||||
|
func AllowNone() Option {
|
||||||
|
return allowed(0)
|
||||||
|
}
|
||||||
|
|
||||||
|
func allowed(allowed level) Option {
|
||||||
|
return func(l *filter) { l.allowed = allowed }
|
||||||
|
}
|
||||||
|
|
||||||
|
// ErrNotAllowed sets the error to return from Log when it squelches a log
|
||||||
|
// event disallowed by the configured Allow[Level] option. By default,
|
||||||
|
// ErrNotAllowed is nil; in this case the log event is squelched with no
|
||||||
|
// error.
|
||||||
|
func ErrNotAllowed(err error) Option {
|
||||||
|
return func(l *filter) { l.errNotAllowed = err }
|
||||||
|
}
|
||||||
|
|
||||||
|
type level byte
|
||||||
|
|
||||||
|
const (
|
||||||
|
levelDebug level = 1 << iota
|
||||||
|
levelInfo
|
||||||
|
levelError
|
||||||
|
)
|
103
log/filter_test.go
Normal file
103
log/filter_test.go
Normal file
@ -0,0 +1,103 @@
|
|||||||
|
package log_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/tendermint/tmlibs/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestVariousLevels(t *testing.T) {
|
||||||
|
testCases := []struct {
|
||||||
|
name string
|
||||||
|
allowed log.Option
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"AllowAll",
|
||||||
|
log.AllowAll(),
|
||||||
|
strings.Join([]string{
|
||||||
|
`{"_msg":"here","level":"debug","this is":"debug log"}`,
|
||||||
|
`{"_msg":"here","level":"info","this is":"info log"}`,
|
||||||
|
`{"_msg":"here","level":"error","this is":"error log"}`,
|
||||||
|
}, "\n"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"AllowDebug",
|
||||||
|
log.AllowDebug(),
|
||||||
|
strings.Join([]string{
|
||||||
|
`{"_msg":"here","level":"debug","this is":"debug log"}`,
|
||||||
|
`{"_msg":"here","level":"info","this is":"info log"}`,
|
||||||
|
`{"_msg":"here","level":"error","this is":"error log"}`,
|
||||||
|
}, "\n"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"AllowInfo",
|
||||||
|
log.AllowInfo(),
|
||||||
|
strings.Join([]string{
|
||||||
|
`{"_msg":"here","level":"info","this is":"info log"}`,
|
||||||
|
`{"_msg":"here","level":"error","this is":"error log"}`,
|
||||||
|
}, "\n"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"AllowError",
|
||||||
|
log.AllowError(),
|
||||||
|
strings.Join([]string{
|
||||||
|
`{"_msg":"here","level":"error","this is":"error log"}`,
|
||||||
|
}, "\n"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"AllowNone",
|
||||||
|
log.AllowNone(),
|
||||||
|
``,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range testCases {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
logger := log.NewFilter(log.NewTMJSONLogger(&buf), tc.allowed)
|
||||||
|
|
||||||
|
logger.Debug("here", "this is", "debug log")
|
||||||
|
logger.Info("here", "this is", "info log")
|
||||||
|
logger.Error("here", "this is", "error log")
|
||||||
|
|
||||||
|
if want, have := tc.want, strings.TrimSpace(buf.String()); want != have {
|
||||||
|
t.Errorf("\nwant:\n%s\nhave:\n%s", want, have)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestErrNotAllowed(t *testing.T) {
|
||||||
|
myError := errors.New("squelched!")
|
||||||
|
opts := []log.Option{
|
||||||
|
log.AllowError(),
|
||||||
|
log.ErrNotAllowed(myError),
|
||||||
|
}
|
||||||
|
logger := log.NewFilter(log.NewNopLogger(), opts...)
|
||||||
|
|
||||||
|
if want, have := myError, logger.Info("foo", "bar", "baz"); want != have {
|
||||||
|
t.Errorf("want %#+v, have %#+v", want, have)
|
||||||
|
}
|
||||||
|
|
||||||
|
if want, have := error(nil), logger.Error("foo", "bar", "baz"); want != have {
|
||||||
|
t.Errorf("want %#+v, have %#+v", want, have)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLevelContext(t *testing.T) {
|
||||||
|
var buf bytes.Buffer
|
||||||
|
|
||||||
|
var logger log.Logger
|
||||||
|
logger = log.NewTMJSONLogger(&buf)
|
||||||
|
logger = log.NewFilter(logger, log.AllowAll())
|
||||||
|
logger = logger.With("context", "value")
|
||||||
|
|
||||||
|
logger.Info("foo", "bar", "baz")
|
||||||
|
if want, have := `{"_msg":"foo","bar":"baz","context":"value","level":"info"}`, strings.TrimSpace(buf.String()); want != have {
|
||||||
|
t.Errorf("\nwant '%s'\nhave '%s'", want, have)
|
||||||
|
}
|
||||||
|
}
|
@ -13,7 +13,6 @@ type Logger interface {
|
|||||||
Error(msg string, keyvals ...interface{}) error
|
Error(msg string, keyvals ...interface{}) error
|
||||||
|
|
||||||
With(keyvals ...interface{}) Logger
|
With(keyvals ...interface{}) Logger
|
||||||
WithLevel(lvl string) Logger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSyncWriter returns a new writer that is safe for concurrent use by
|
// NewSyncWriter returns a new writer that is safe for concurrent use by
|
||||||
|
@ -23,7 +23,3 @@ func (nopLogger) Error(string, ...interface{}) error {
|
|||||||
func (l *nopLogger) With(...interface{}) Logger {
|
func (l *nopLogger) With(...interface{}) Logger {
|
||||||
return l
|
return l
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *nopLogger) WithLevel(lvl string) Logger {
|
|
||||||
return l
|
|
||||||
}
|
|
||||||
|
15
log/tm_json_logger.go
Normal file
15
log/tm_json_logger.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io"
|
||||||
|
|
||||||
|
kitlog "github.com/go-kit/kit/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NewTMJSONLogger returns a Logger that encodes keyvals to the Writer as a
|
||||||
|
// single JSON object. Each log event produces no more than one call to
|
||||||
|
// w.Write. The passed Writer must be safe for concurrent use by multiple
|
||||||
|
// goroutines if the returned Logger will be used concurrently.
|
||||||
|
func NewTMJSONLogger(w io.Writer) Logger {
|
||||||
|
return &tmLogger{kitlog.NewJSONLogger(w)}
|
||||||
|
}
|
@ -5,7 +5,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
|
|
||||||
kitlog "github.com/go-kit/kit/log"
|
kitlog "github.com/go-kit/kit/log"
|
||||||
"github.com/go-kit/kit/log/level"
|
kitlevel "github.com/go-kit/kit/log/level"
|
||||||
"github.com/go-kit/kit/log/term"
|
"github.com/go-kit/kit/log/term"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -23,15 +23,13 @@ var _ Logger = (*tmLogger)(nil)
|
|||||||
// NewTMTermLogger returns a logger that encodes msg and keyvals to the Writer
|
// NewTMTermLogger returns a logger that encodes msg and keyvals to the Writer
|
||||||
// using go-kit's log as an underlying logger and our custom formatter. Note
|
// using go-kit's log as an underlying logger and our custom formatter. Note
|
||||||
// that underlying logger could be swapped with something else.
|
// that underlying logger could be swapped with something else.
|
||||||
//
|
|
||||||
// Default logging level is info. You can change it using SetLevel().
|
|
||||||
func NewTMLogger(w io.Writer) Logger {
|
func NewTMLogger(w io.Writer) Logger {
|
||||||
// Color by level value
|
// Color by level value
|
||||||
colorFn := func(keyvals ...interface{}) term.FgBgColor {
|
colorFn := func(keyvals ...interface{}) term.FgBgColor {
|
||||||
if keyvals[0] != level.Key() {
|
if keyvals[0] != kitlevel.Key() {
|
||||||
panic(fmt.Sprintf("expected level key to be first, got %v", keyvals[0]))
|
panic(fmt.Sprintf("expected level key to be first, got %v", keyvals[0]))
|
||||||
}
|
}
|
||||||
switch keyvals[1].(level.Value).String() {
|
switch keyvals[1].(kitlevel.Value).String() {
|
||||||
case "debug":
|
case "debug":
|
||||||
return term.FgBgColor{Fg: term.DarkGray}
|
return term.FgBgColor{Fg: term.DarkGray}
|
||||||
case "error":
|
case "error":
|
||||||
@ -41,26 +39,24 @@ func NewTMLogger(w io.Writer) Logger {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
srcLogger := term.NewLogger(w, NewTMFmtLogger, colorFn)
|
return &tmLogger{term.NewLogger(w, NewTMFmtLogger, colorFn)}
|
||||||
srcLogger = level.NewFilter(srcLogger, level.AllowInfo())
|
|
||||||
return &tmLogger{srcLogger}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Info logs a message at level Info.
|
// Info logs a message at level Info.
|
||||||
func (l *tmLogger) Info(msg string, keyvals ...interface{}) error {
|
func (l *tmLogger) Info(msg string, keyvals ...interface{}) error {
|
||||||
lWithLevel := level.Info(l.srcLogger)
|
lWithLevel := kitlevel.Info(l.srcLogger)
|
||||||
return kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...)
|
return kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Debug logs a message at level Debug.
|
// Debug logs a message at level Debug.
|
||||||
func (l *tmLogger) Debug(msg string, keyvals ...interface{}) error {
|
func (l *tmLogger) Debug(msg string, keyvals ...interface{}) error {
|
||||||
lWithLevel := level.Debug(l.srcLogger)
|
lWithLevel := kitlevel.Debug(l.srcLogger)
|
||||||
return kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...)
|
return kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error logs a message at level Error.
|
// Error logs a message at level Error.
|
||||||
func (l *tmLogger) Error(msg string, keyvals ...interface{}) error {
|
func (l *tmLogger) Error(msg string, keyvals ...interface{}) error {
|
||||||
lWithLevel := level.Error(l.srcLogger)
|
lWithLevel := kitlevel.Error(l.srcLogger)
|
||||||
return kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...)
|
return kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,17 +65,3 @@ func (l *tmLogger) Error(msg string, keyvals ...interface{}) error {
|
|||||||
func (l *tmLogger) With(keyvals ...interface{}) Logger {
|
func (l *tmLogger) With(keyvals ...interface{}) Logger {
|
||||||
return &tmLogger{kitlog.With(l.srcLogger, keyvals...)}
|
return &tmLogger{kitlog.With(l.srcLogger, keyvals...)}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithLevel returns a new logger with the level set to lvl.
|
|
||||||
func (l *tmLogger) WithLevel(lvl string) Logger {
|
|
||||||
switch lvl {
|
|
||||||
case "info":
|
|
||||||
return &tmLogger{level.NewFilter(l.srcLogger, level.AllowInfo())}
|
|
||||||
case "debug":
|
|
||||||
return &tmLogger{level.NewFilter(l.srcLogger, level.AllowDebug())}
|
|
||||||
case "error":
|
|
||||||
return &tmLogger{level.NewFilter(l.srcLogger, level.AllowError())}
|
|
||||||
default:
|
|
||||||
panic(fmt.Sprintf("Unexpected level %v, expect either \"info\" or \"debug\" or \"error\"", lvl))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -8,7 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
kitlog "github.com/go-kit/kit/log"
|
kitlog "github.com/go-kit/kit/log"
|
||||||
"github.com/go-kit/kit/log/level"
|
kitlevel "github.com/go-kit/kit/log/level"
|
||||||
"github.com/go-logfmt/logfmt"
|
"github.com/go-logfmt/logfmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -56,13 +56,13 @@ func (l tmfmtLogger) Log(keyvals ...interface{}) error {
|
|||||||
|
|
||||||
for i := 0; i < len(keyvals)-1; i += 2 {
|
for i := 0; i < len(keyvals)-1; i += 2 {
|
||||||
// Extract level
|
// Extract level
|
||||||
if keyvals[i] == level.Key() {
|
if keyvals[i] == kitlevel.Key() {
|
||||||
lvlIndex = i
|
lvlIndex = i
|
||||||
switch keyvals[i+1].(type) {
|
switch keyvals[i+1].(type) {
|
||||||
case string:
|
case string:
|
||||||
lvl = keyvals[i+1].(string)
|
lvl = keyvals[i+1].(string)
|
||||||
case level.Value:
|
case kitlevel.Value:
|
||||||
lvl = keyvals[i+1].(level.Value).String()
|
lvl = keyvals[i+1].(kitlevel.Value).String()
|
||||||
default:
|
default:
|
||||||
panic(fmt.Sprintf("level value of unknown type %T", keyvals[i+1]))
|
panic(fmt.Sprintf("level value of unknown type %T", keyvals[i+1]))
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user