@ -1,3 +1,2 @@ | |||
*.swp | |||
*.swo | |||
vendor | |||
.glide |
@ -1,7 +0,0 @@ | |||
package events | |||
import ( | |||
"github.com/tendermint/tmlibs/logger" | |||
) | |||
var log = logger.New("module", "events") |
@ -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, info and debug level log events to pass. | |||
func AllowDebug() Option { | |||
return allowed(levelError | levelInfo | levelDebug) | |||
} | |||
// AllowInfo allows error 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 | |||
) |
@ -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) | |||
} | |||
} |
@ -0,0 +1,30 @@ | |||
package log | |||
import ( | |||
"io" | |||
kitlog "github.com/go-kit/kit/log" | |||
) | |||
// Logger is what any Tendermint library should take. | |||
type Logger interface { | |||
Debug(msg string, keyvals ...interface{}) error | |||
Info(msg string, keyvals ...interface{}) error | |||
Error(msg string, keyvals ...interface{}) error | |||
With(keyvals ...interface{}) Logger | |||
} | |||
// NewSyncWriter returns a new writer that is safe for concurrent use by | |||
// multiple goroutines. Writes to the returned writer are passed on to w. If | |||
// another write is already in progress, the calling goroutine blocks until | |||
// the writer is available. | |||
// | |||
// If w implements the following interface, so does the returned writer. | |||
// | |||
// interface { | |||
// Fd() uintptr | |||
// } | |||
func NewSyncWriter(w io.Writer) io.Writer { | |||
return kitlog.NewSyncWriter(w) | |||
} |
@ -0,0 +1,25 @@ | |||
package log | |||
type nopLogger struct{} | |||
// Interface assertions | |||
var _ Logger = (*nopLogger)(nil) | |||
// NewNopLogger returns a logger that doesn't do anything. | |||
func NewNopLogger() Logger { return &nopLogger{} } | |||
func (nopLogger) Info(string, ...interface{}) error { | |||
return nil | |||
} | |||
func (nopLogger) Debug(string, ...interface{}) error { | |||
return nil | |||
} | |||
func (nopLogger) Error(string, ...interface{}) error { | |||
return nil | |||
} | |||
func (l *nopLogger) With(...interface{}) Logger { | |||
return l | |||
} |
@ -0,0 +1,18 @@ | |||
package log_test | |||
import ( | |||
"testing" | |||
"github.com/tendermint/tmlibs/log" | |||
) | |||
func TestNopLogger(t *testing.T) { | |||
t.Parallel() | |||
logger := log.NewNopLogger() | |||
if err := logger.Info("Hello", "abc", 123); err != nil { | |||
t.Error(err) | |||
} | |||
if err := logger.With("def", "ghi").Debug(""); err != nil { | |||
t.Error(err) | |||
} | |||
} |
@ -0,0 +1,31 @@ | |||
package log | |||
import ( | |||
"os" | |||
"testing" | |||
) | |||
var ( | |||
// reuse the same logger across all tests | |||
_testingLogger Logger | |||
) | |||
// TestingLogger returns a TMLogger which writes to STDOUT if testing being run | |||
// with the verbose (-v) flag, NopLogger otherwise. | |||
// | |||
// Note that the call to TestingLogger() must be made | |||
// inside a test (not in the init func) because | |||
// verbose flag only set at the time of testing. | |||
func TestingLogger() Logger { | |||
if _testingLogger != nil { | |||
return _testingLogger | |||
} | |||
if testing.Verbose() { | |||
_testingLogger = NewTMLogger(NewSyncWriter(os.Stdout)) | |||
} else { | |||
_testingLogger = NewNopLogger() | |||
} | |||
return _testingLogger | |||
} |
@ -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)} | |||
} |
@ -0,0 +1,67 @@ | |||
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 | |||
) | |||
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)} | |||
} | |||
// Info logs a message at level Info. | |||
func (l *tmLogger) Info(msg string, keyvals ...interface{}) error { | |||
lWithLevel := kitlevel.Info(l.srcLogger) | |||
return kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...) | |||
} | |||
// Debug logs a message at level Debug. | |||
func (l *tmLogger) Debug(msg string, keyvals ...interface{}) error { | |||
lWithLevel := kitlevel.Debug(l.srcLogger) | |||
return kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...) | |||
} | |||
// Error logs a message at level Error. | |||
func (l *tmLogger) Error(msg string, keyvals ...interface{}) error { | |||
lWithLevel := kitlevel.Error(l.srcLogger) | |||
return 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...)} | |||
} |
@ -0,0 +1,41 @@ | |||
package log_test | |||
import ( | |||
"io/ioutil" | |||
"testing" | |||
"github.com/tendermint/tmlibs/log" | |||
) | |||
func TestTMLogger(t *testing.T) { | |||
t.Parallel() | |||
logger := log.NewTMLogger(ioutil.Discard) | |||
if err := logger.Info("Hello", "abc", 123); err != nil { | |||
t.Error(err) | |||
} | |||
if err := logger.With("def", "ghi").Debug(""); err != nil { | |||
t.Error(err) | |||
} | |||
} | |||
func BenchmarkTMLoggerSimple(b *testing.B) { | |||
benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), baseInfoMessage) | |||
} | |||
func BenchmarkTMLoggerContextual(b *testing.B) { | |||
benchmarkRunner(b, log.NewTMLogger(ioutil.Discard), withInfoMessage) | |||
} | |||
func benchmarkRunner(b *testing.B, logger log.Logger, f func(log.Logger)) { | |||
lc := logger.With("common_key", "common_value") | |||
b.ReportAllocs() | |||
b.ResetTimer() | |||
for i := 0; i < b.N; i++ { | |||
f(lc) | |||
} | |||
} | |||
var ( | |||
baseInfoMessage = func(logger log.Logger) { logger.Info("foo_message", "foo_key", "foo_value") } | |||
withInfoMessage = func(logger log.Logger) { logger.With("a", "b").Info("c", "d", "f") } | |||
) |
@ -0,0 +1,116 @@ | |||
package log | |||
import ( | |||
"bytes" | |||
"fmt" | |||
"io" | |||
"sync" | |||
"time" | |||
kitlog "github.com/go-kit/kit/log" | |||
kitlevel "github.com/go-kit/kit/log/level" | |||
"github.com/go-logfmt/logfmt" | |||
) | |||
type tmfmtEncoder struct { | |||
*logfmt.Encoder | |||
buf bytes.Buffer | |||
} | |||
func (l *tmfmtEncoder) Reset() { | |||
l.Encoder.Reset() | |||
l.buf.Reset() | |||
} | |||
var tmfmtEncoderPool = sync.Pool{ | |||
New: func() interface{} { | |||
var enc tmfmtEncoder | |||
enc.Encoder = logfmt.NewEncoder(&enc.buf) | |||
return &enc | |||
}, | |||
} | |||
type tmfmtLogger struct { | |||
w io.Writer | |||
} | |||
// NewTMFmtLogger returns a logger that encodes keyvals to the Writer in | |||
// Tendermint custom format. | |||
// | |||
// 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 NewTMFmtLogger(w io.Writer) kitlog.Logger { | |||
return &tmfmtLogger{w} | |||
} | |||
func (l tmfmtLogger) Log(keyvals ...interface{}) error { | |||
enc := tmfmtEncoderPool.Get().(*tmfmtEncoder) | |||
enc.Reset() | |||
defer tmfmtEncoderPool.Put(enc) | |||
lvl := "none" | |||
msg := "unknown" | |||
lvlIndex := -1 | |||
msgIndex := -1 | |||
for i := 0; i < len(keyvals)-1; i += 2 { | |||
// Extract level | |||
if keyvals[i] == kitlevel.Key() { | |||
lvlIndex = i | |||
switch keyvals[i+1].(type) { | |||
case string: | |||
lvl = keyvals[i+1].(string) | |||
case kitlevel.Value: | |||
lvl = keyvals[i+1].(kitlevel.Value).String() | |||
default: | |||
panic(fmt.Sprintf("level value of unknown type %T", keyvals[i+1])) | |||
} | |||
continue | |||
} | |||
// and message | |||
if keyvals[i] == msgKey { | |||
msgIndex = i | |||
msg = keyvals[i+1].(string) | |||
continue | |||
} | |||
if lvlIndex > 0 && msgIndex > 0 { // found all we're looking for | |||
break | |||
} | |||
} | |||
// Form a custom Tendermint line | |||
// | |||
// Example: | |||
// D[05-02|11:06:44.322] Stopping AddrBook (ignoring: already stopped) | |||
// | |||
// Description: | |||
// D - first character of the level, uppercase (ASCII only) | |||
// [05-02|11:06:44.322] - our time format (see https://golang.org/src/time/format.go) | |||
// Stopping ... - message | |||
enc.buf.WriteString(fmt.Sprintf("%c[%s] %-44s", lvl[0]-32, time.Now().UTC().Format("01-02|15:04:05.000"), msg)) | |||
for i := 0; i < len(keyvals)-1; i += 2 { | |||
if i == lvlIndex || i == msgIndex { | |||
continue | |||
} | |||
if err := enc.EncodeKeyval(keyvals[i], keyvals[i+1]); err != nil { | |||
return err | |||
} | |||
} | |||
// Add newline to the end of the buffer | |||
if err := enc.EndRecord(); err != nil { | |||
return err | |||
} | |||
// The Logger interface requires implementations to be safe for concurrent | |||
// use by multiple goroutines. For this implementation that means making | |||
// only one call to l.w.Write() for each call to Log. | |||
if _, err := l.w.Write(enc.buf.Bytes()); err != nil { | |||
return err | |||
} | |||
return nil | |||
} |
@ -0,0 +1,110 @@ | |||
package log_test | |||
import ( | |||
"bytes" | |||
"errors" | |||
"io/ioutil" | |||
"math" | |||
"regexp" | |||
"testing" | |||
kitlog "github.com/go-kit/kit/log" | |||
"github.com/stretchr/testify/assert" | |||
"github.com/tendermint/tmlibs/log" | |||
) | |||
func TestTMFmtLogger(t *testing.T) { | |||
t.Parallel() | |||
buf := &bytes.Buffer{} | |||
logger := log.NewTMFmtLogger(buf) | |||
if err := logger.Log("hello", "world"); err != nil { | |||
t.Fatal(err) | |||
} | |||
assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+ hello=world\n$`), buf.String()) | |||
buf.Reset() | |||
if err := logger.Log("a", 1, "err", errors.New("error")); err != nil { | |||
t.Fatal(err) | |||
} | |||
assert.Regexp(t, regexp.MustCompile(`N\[.+\] unknown \s+ a=1 err=error\n$`), buf.String()) | |||
buf.Reset() | |||
err := logger.Log("std_map", map[int]int{1: 2}, "my_map", mymap{0: 0}) | |||
assert.NotNil(t, err) | |||
buf.Reset() | |||
if err := logger.Log("level", "error"); err != nil { | |||
t.Fatal(err) | |||
} | |||
assert.Regexp(t, regexp.MustCompile(`E\[.+\] unknown \s+\n$`), buf.String()) | |||
buf.Reset() | |||
if err := logger.Log("_msg", "Hello"); err != nil { | |||
t.Fatal(err) | |||
} | |||
assert.Regexp(t, regexp.MustCompile(`N\[.+\] Hello \s+\n$`), buf.String()) | |||
} | |||
func BenchmarkTMFmtLoggerSimple(b *testing.B) { | |||
benchmarkRunnerKitlog(b, log.NewTMFmtLogger(ioutil.Discard), baseMessage) | |||
} | |||
func BenchmarkTMFmtLoggerContextual(b *testing.B) { | |||
benchmarkRunnerKitlog(b, log.NewTMFmtLogger(ioutil.Discard), withMessage) | |||
} | |||
func TestTMFmtLoggerConcurrency(t *testing.T) { | |||
t.Parallel() | |||
testConcurrency(t, log.NewTMFmtLogger(ioutil.Discard), 10000) | |||
} | |||
func benchmarkRunnerKitlog(b *testing.B, logger kitlog.Logger, f func(kitlog.Logger)) { | |||
lc := kitlog.With(logger, "common_key", "common_value") | |||
b.ReportAllocs() | |||
b.ResetTimer() | |||
for i := 0; i < b.N; i++ { | |||
f(lc) | |||
} | |||
} | |||
var ( | |||
baseMessage = func(logger kitlog.Logger) { logger.Log("foo_key", "foo_value") } | |||
withMessage = func(logger kitlog.Logger) { kitlog.With(logger, "a", "b").Log("d", "f") } | |||
) | |||
// These test are designed to be run with the race detector. | |||
func testConcurrency(t *testing.T, logger kitlog.Logger, total int) { | |||
n := int(math.Sqrt(float64(total))) | |||
share := total / n | |||
errC := make(chan error, n) | |||
for i := 0; i < n; i++ { | |||
go func() { | |||
errC <- spam(logger, share) | |||
}() | |||
} | |||
for i := 0; i < n; i++ { | |||
err := <-errC | |||
if err != nil { | |||
t.Fatalf("concurrent logging error: %v", err) | |||
} | |||
} | |||
} | |||
func spam(logger kitlog.Logger, count int) error { | |||
for i := 0; i < count; i++ { | |||
err := logger.Log("key", i) | |||
if err != nil { | |||
return err | |||
} | |||
} | |||
return nil | |||
} | |||
type mymap map[int]int | |||
func (m mymap) String() string { return "special_behavior" } |