mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-29 00:32:14 +00:00
See https://github.com/go-kit/kit/issues/164 for discussion of why kitlog returns an error. ``` Package log is designed to be used for more than simple application info/warning/error logging; it's suitable for log-structured data in an e.g. Lambda architecture, where each invocation is important. I agree with you that if we were doing only application logging the error would be more noise than signal. But the scope of the package is larger than that. ``` Since we are doing only application logging and we're not checking errors, it is safe to get rid them.
75 lines
2.1 KiB
Go
75 lines
2.1 KiB
Go
package log
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
|
|
kitlog "github.com/go-kit/kit/log"
|
|
kitlevel "github.com/go-kit/kit/log/level"
|
|
"github.com/go-kit/kit/log/term"
|
|
)
|
|
|
|
const (
|
|
msgKey = "_msg" // "_" prefixed to avoid collisions
|
|
moduleKey = "module"
|
|
)
|
|
|
|
type tmLogger struct {
|
|
srcLogger kitlog.Logger
|
|
}
|
|
|
|
// Interface assertions
|
|
var _ Logger = (*tmLogger)(nil)
|
|
|
|
// 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
|
|
// that underlying logger could be swapped with something else.
|
|
func NewTMLogger(w io.Writer) Logger {
|
|
// Color by level value
|
|
colorFn := func(keyvals ...interface{}) term.FgBgColor {
|
|
if keyvals[0] != kitlevel.Key() {
|
|
panic(fmt.Sprintf("expected level key to be first, got %v", keyvals[0]))
|
|
}
|
|
switch keyvals[1].(kitlevel.Value).String() {
|
|
case "debug":
|
|
return term.FgBgColor{Fg: term.DarkGray}
|
|
case "error":
|
|
return term.FgBgColor{Fg: term.Red}
|
|
default:
|
|
return term.FgBgColor{}
|
|
}
|
|
}
|
|
|
|
return &tmLogger{term.NewLogger(w, NewTMFmtLogger, colorFn)}
|
|
}
|
|
|
|
// NewTMLoggerWithColorFn allows you to provide your own color function. See
|
|
// NewTMLogger for documentation.
|
|
func NewTMLoggerWithColorFn(w io.Writer, colorFn func(keyvals ...interface{}) term.FgBgColor) Logger {
|
|
return &tmLogger{term.NewLogger(w, NewTMFmtLogger, colorFn)}
|
|
}
|
|
|
|
// Info logs a message at level Info.
|
|
func (l *tmLogger) Info(msg string, keyvals ...interface{}) {
|
|
lWithLevel := kitlevel.Info(l.srcLogger)
|
|
kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...)
|
|
}
|
|
|
|
// Debug logs a message at level Debug.
|
|
func (l *tmLogger) Debug(msg string, keyvals ...interface{}) {
|
|
lWithLevel := kitlevel.Debug(l.srcLogger)
|
|
kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...)
|
|
}
|
|
|
|
// Error logs a message at level Error.
|
|
func (l *tmLogger) Error(msg string, keyvals ...interface{}) {
|
|
lWithLevel := kitlevel.Error(l.srcLogger)
|
|
kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...)
|
|
}
|
|
|
|
// With returns a new contextual logger with keyvals prepended to those passed
|
|
// to calls to Info, Debug or Error.
|
|
func (l *tmLogger) With(keyvals ...interface{}) Logger {
|
|
return &tmLogger{kitlog.With(l.srcLogger, keyvals...)}
|
|
}
|