From ed76afd409058b162748bb77232d584ea89fe498 Mon Sep 17 00:00:00 2001 From: Anton Kaliaev Date: Thu, 27 Apr 2017 13:42:03 +0400 Subject: [PATCH] Logger interface and tmLogger impl based on go-kit --- .gitignore | 3 +- common/service.go | 56 +++++++++---------- events/events.go | 2 +- events/log.go | 7 --- glide.lock | 40 +++++++------- glide.yaml | 16 +++++- log/logger.go | 27 +++++++++ log/nop_logger.go | 18 ++++++ log/nop_logger_test.go | 18 ++++++ log/tm_logger.go | 74 +++++++++++++++++++++++++ log/tm_logger_test.go | 41 ++++++++++++++ log/tmfmt_logger.go | 116 +++++++++++++++++++++++++++++++++++++++ log/tmfmt_logger_test.go | 110 +++++++++++++++++++++++++++++++++++++ logger/log.go | 11 ++-- 14 files changed, 472 insertions(+), 67 deletions(-) delete mode 100644 events/log.go create mode 100644 log/logger.go create mode 100644 log/nop_logger.go create mode 100644 log/nop_logger_test.go create mode 100644 log/tm_logger.go create mode 100644 log/tm_logger_test.go create mode 100644 log/tmfmt_logger.go create mode 100644 log/tmfmt_logger_test.go diff --git a/.gitignore b/.gitignore index f37225baa..62f28681c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ -*.swp -*.swo vendor +.glide diff --git a/common/service.go b/common/service.go index 6a274a798..71fc03cb9 100644 --- a/common/service.go +++ b/common/service.go @@ -3,7 +3,7 @@ package common import ( "sync/atomic" - "github.com/tendermint/log15" + "github.com/tendermint/tmlibs/log" ) type Service interface { @@ -19,6 +19,8 @@ type Service interface { IsRunning() bool String() string + + SetLogger(log.Logger) } /* @@ -64,7 +66,7 @@ Typical usage: } */ type BaseService struct { - log log15.Logger + Logger log.Logger name string started uint32 // atomic stopped uint32 // atomic @@ -74,27 +76,31 @@ type BaseService struct { impl Service } -func NewBaseService(log log15.Logger, name string, impl Service) *BaseService { +func NewBaseService(logger log.Logger, name string, impl Service) *BaseService { + if logger == nil { + logger = log.NewNopLogger() + } + return &BaseService{ - log: log, - name: name, - Quit: make(chan struct{}), - impl: impl, + Logger: logger, + name: name, + Quit: make(chan struct{}), + impl: impl, } } +func (bs *BaseService) SetLogger(l log.Logger) { + bs.Logger = l +} + // Implements Servce func (bs *BaseService) Start() (bool, error) { if atomic.CompareAndSwapUint32(&bs.started, 0, 1) { if atomic.LoadUint32(&bs.stopped) == 1 { - if bs.log != nil { - bs.log.Warn(Fmt("Not starting %v -- already stopped", bs.name), "impl", bs.impl) - } + bs.Logger.Error(Fmt("Not starting %v -- already stopped", bs.name), "impl", bs.impl) return false, nil } else { - if bs.log != nil { - bs.log.Info(Fmt("Starting %v", bs.name), "impl", bs.impl) - } + bs.Logger.Info(Fmt("Starting %v", bs.name), "impl", bs.impl) } err := bs.impl.OnStart() if err != nil { @@ -104,9 +110,7 @@ func (bs *BaseService) Start() (bool, error) { } return true, err } else { - if bs.log != nil { - bs.log.Debug(Fmt("Not starting %v -- already started", bs.name), "impl", bs.impl) - } + bs.Logger.Debug(Fmt("Not starting %v -- already started", bs.name), "impl", bs.impl) return false, nil } } @@ -119,16 +123,12 @@ func (bs *BaseService) OnStart() error { return nil } // Implements Service func (bs *BaseService) Stop() bool { if atomic.CompareAndSwapUint32(&bs.stopped, 0, 1) { - if bs.log != nil { - bs.log.Info(Fmt("Stopping %v", bs.name), "impl", bs.impl) - } + bs.Logger.Info(Fmt("Stopping %v", bs.name), "impl", bs.impl) bs.impl.OnStop() close(bs.Quit) return true } else { - if bs.log != nil { - bs.log.Debug(Fmt("Stopping %v (ignoring: already stopped)", bs.name), "impl", bs.impl) - } + bs.Logger.Debug(Fmt("Stopping %v (ignoring: already stopped)", bs.name), "impl", bs.impl) return false } } @@ -147,9 +147,7 @@ func (bs *BaseService) Reset() (bool, error) { bs.Quit = make(chan struct{}) return true, bs.impl.OnReset() } else { - if bs.log != nil { - bs.log.Debug(Fmt("Can't reset %v. Not stopped", bs.name), "impl", bs.impl) - } + bs.Logger.Debug(Fmt("Can't reset %v. Not stopped", bs.name), "impl", bs.impl) return false, nil } // never happens @@ -182,11 +180,11 @@ type QuitService struct { BaseService } -func NewQuitService(log log15.Logger, name string, impl Service) *QuitService { - if log != nil { - log.Warn("QuitService is deprecated, use BaseService instead") +func NewQuitService(logger log.Logger, name string, impl Service) *QuitService { + if logger != nil { + logger.Info("QuitService is deprecated, use BaseService instead") } return &QuitService{ - BaseService: *NewBaseService(log, name, impl), + BaseService: *NewBaseService(logger, name, impl), } } diff --git a/events/events.go b/events/events.go index 487d120b3..12aa07813 100644 --- a/events/events.go +++ b/events/events.go @@ -45,7 +45,7 @@ type eventSwitch struct { func NewEventSwitch() EventSwitch { evsw := &eventSwitch{} - evsw.BaseService = *NewBaseService(log, "EventSwitch", evsw) + evsw.BaseService = *NewBaseService(nil, "EventSwitch", evsw) return evsw } diff --git a/events/log.go b/events/log.go deleted file mode 100644 index adb6bab09..000000000 --- a/events/log.go +++ /dev/null @@ -1,7 +0,0 @@ -package events - -import ( - "github.com/tendermint/tmlibs/logger" -) - -var log = logger.New("module", "events") diff --git a/glide.lock b/glide.lock index b7d4fb8ff..37fe6ba4b 100644 --- a/glide.lock +++ b/glide.lock @@ -1,5 +1,5 @@ -hash: a28817fffc1bfbba980a957b7782a84ea574fb73d5dfb01730f7e304c9dee630 -updated: 2017-05-03T10:27:41.060683376+02:00 +hash: 69359a39dbb6957c9f09167520317ad72d4bfa75f37a614b347e2510768c8a42 +updated: 2017-05-05T17:40:30.424309209Z imports: - name: github.com/fsnotify/fsnotify version: 4da3e2cfbabc9f751898f250b49f2439785783a1 @@ -12,11 +12,11 @@ imports: - name: github.com/go-logfmt/logfmt version: 390ab7935ee28ec6b286364bba9b4dd6410cb3d5 - name: github.com/go-stack/stack - version: 100eb0c0a9c5b306ca2fb4f165df21d80ada4b82 + version: 7a2f19628aabfe68f0766b59e74d6315f8347d22 - name: github.com/golang/snappy version: 553a641470496b2327abcac10b36396bd98e45c9 - name: github.com/hashicorp/hcl - version: 630949a3c5fa3c613328e1b8256052cbc2327c9b + version: a4b07c25de5ff55ad3b8936cea69a79a3d95a855 subpackages: - hcl/ast - hcl/parser @@ -35,17 +35,17 @@ imports: - name: github.com/magiconair/properties version: 51463bfca2576e06c62a8504b5c0f06d61312647 - name: github.com/mattn/go-colorable - version: d228849504861217f796da67fae4f6e347643f15 + version: ded68f7a9561c023e790de24279db7ebf473ea80 - name: github.com/mattn/go-isatty - version: 30a891c33c7cde7b02a981314b4228ec99380cca + version: fc9e8d8ef48496124e79ae0df75490096eccf6fe - name: github.com/mitchellh/mapstructure - version: 53818660ed4955e899c0bcafa97299a388bd7c8e + version: cc8532a8e9a55ea36402aa21efdf403a60d34096 - name: github.com/pelletier/go-buffruneio version: c37440a7cf42ac63b919c752ca73a85067e05992 - name: github.com/pelletier/go-toml - version: 13d49d4606eb801b8f01ae542b4afc4c6ee3d84a + version: 97253b98df84f9eef872866d079e74b8265150f1 - name: github.com/pkg/errors - version: bfd5150e4e41705ded2129ec33379de1cb90b513 + version: c605e284fe17294bda444b34710735b29d1a9d90 - name: github.com/spf13/afero version: 9be650865eab0c12963d8753212f4f9c66cdcf12 subpackages: @@ -53,15 +53,15 @@ imports: - name: github.com/spf13/cast version: acbeb36b902d72a7a4c18e8f3241075e7ab763e4 - name: github.com/spf13/cobra - version: fcd0c5a1df88f5d6784cb4feead962c3f3d0b66c + version: db6b9a8b3f3f400c8ecb4a4d7d02245b8facad66 - name: github.com/spf13/jwalterweatherman version: fa7ca7e836cf3a8bb4ebf799f472c12d7e903d66 - name: github.com/spf13/pflag - version: 9ff6c6923cfffbcd502984b8e0c80539a94968b7 + version: 80fe0fb4eba54167e2ccae1c6c950e72abf61b73 - name: github.com/spf13/viper - version: 5d46e70da8c0b6f812e0b170b7a985753b5c63cb + version: 0967fc9aceab2ce9da34061253ac10fb99bba5b2 - name: github.com/syndtr/goleveldb - version: 23851d93a2292dcc56e71a18ec9e0624d84a0f65 + version: 8c81ea47d4c41a385645e133e15510fc6a2a74b4 subpackages: - leveldb - leveldb/cache @@ -76,24 +76,24 @@ imports: - leveldb/table - leveldb/util - name: github.com/tendermint/go-wire - version: 334005c236d19c632fb5f073f9de3b0fab6a522b + version: b53add0b622662731985485f3a19be7f684660b8 subpackages: - data - data/base58 - name: github.com/tendermint/log15 - version: ae0f3d6450da9eac7074b439c8e1c3cabf0d5ce6 + version: f91285dece9f4875421b481da3e613d83d44f29b subpackages: - term - name: golang.org/x/crypto - version: 7c6cc321c680f03b9ef0764448e780704f486b51 + version: 5a033cc77e57eca05bdb50522851d29e03569cbe subpackages: - ripemd160 - name: golang.org/x/sys - version: d75a52659825e75fff6158388dddc6a5b04f9ba5 + version: 9ccfe848b9db8435a24c424abbc07a921adf1df5 subpackages: - unix - name: golang.org/x/text - version: f4b4367115ec2de254587813edaa901bc1c723a8 + version: 470f45bf29f4147d6fbd7dfd0a02a848e49f5bf4 subpackages: - transform - unicode/norm @@ -101,7 +101,7 @@ imports: version: cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b testImports: - name: github.com/davecgh/go-spew - version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9 + version: 04cdfd42973bb9c8589fd6a731800cf222fde1a9 subpackages: - spew - name: github.com/pmezard/go-difflib @@ -109,7 +109,7 @@ testImports: subpackages: - difflib - name: github.com/stretchr/testify - version: 69483b4bd14f5845b5a1e55bca19e954e827f1d0 + version: 4d4bfba8f1d1027c4fdbe371823030df51419987 subpackages: - assert - require diff --git a/glide.yaml b/glide.yaml index b0f22d1a7..2df880175 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,20 +1,30 @@ package: github.com/tendermint/tmlibs import: +- package: github.com/go-kit/kit + subpackages: + - log + - log/level + - log/term +- package: github.com/go-logfmt/logfmt - package: github.com/jmhodges/levigo +- package: github.com/pkg/errors +- package: github.com/spf13/cobra +- package: github.com/spf13/viper - package: github.com/syndtr/goleveldb subpackages: - leveldb - leveldb/errors - leveldb/opt - package: github.com/tendermint/go-wire + subpackages: + - data + - data/base58 - package: github.com/tendermint/log15 - package: golang.org/x/crypto subpackages: - ripemd160 -- package: github.com/go-logfmt/logfmt -- package: github.com/spf13/cobra -- package: github.com/spf13/viper testImport: - package: github.com/stretchr/testify subpackages: - assert + - require diff --git a/log/logger.go b/log/logger.go new file mode 100644 index 000000000..790bcad74 --- /dev/null +++ b/log/logger.go @@ -0,0 +1,27 @@ +package log + +import ( + "fmt" + + 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 returns a new contextual logger with keyvals prepended to those passed +// to calls to Info, Debug or Error. +func With(logger Logger, keyvals ...interface{}) Logger { + switch logger.(type) { + case *tmLogger: + return &tmLogger{kitlog.With(logger.(*tmLogger).srcLogger, keyvals...)} + case *nopLogger: + return logger + default: + panic(fmt.Sprintf("Unexpected logger of type %T", logger)) + } +} diff --git a/log/nop_logger.go b/log/nop_logger.go new file mode 100644 index 000000000..b6e312631 --- /dev/null +++ b/log/nop_logger.go @@ -0,0 +1,18 @@ +package log + +type nopLogger struct{} + +// 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 +} diff --git a/log/nop_logger_test.go b/log/nop_logger_test.go new file mode 100644 index 000000000..9757d4f19 --- /dev/null +++ b/log/nop_logger_test.go @@ -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 := log.With(logger, "def", "ghi").Debug(""); err != nil { + t.Error(err) + } +} diff --git a/log/tm_logger.go b/log/tm_logger.go new file mode 100644 index 000000000..0d01a970e --- /dev/null +++ b/log/tm_logger.go @@ -0,0 +1,74 @@ +package log + +import ( + "fmt" + "io" + + kitlog "github.com/go-kit/kit/log" + "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 +} + +// 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] != level.Key() { + panic(fmt.Sprintf("expected level key to be first, got %v", keyvals[0])) + } + switch keyvals[1].(level.Value).String() { + case "debug": + return term.FgBgColor{Fg: term.DarkGray} + case "error": + return term.FgBgColor{Fg: term.Red} + default: + return term.FgBgColor{} + } + } + + srcLogger := term.NewLogger(w, NewTmfmtLogger, colorFn) + srcLogger = level.NewFilter(srcLogger, level.AllowInfo()) + return &tmLogger{srcLogger} +} + +// WithLevel returns a copy of the logger with a 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)) + } +} + +// Info logs a message at level Info. +func (l *tmLogger) Info(msg string, keyvals ...interface{}) error { + lWithLevel := level.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 := level.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 := level.Error(l.srcLogger) + return kitlog.With(lWithLevel, msgKey, msg).Log(keyvals...) +} diff --git a/log/tm_logger_test.go b/log/tm_logger_test.go new file mode 100644 index 000000000..f301c9fde --- /dev/null +++ b/log/tm_logger_test.go @@ -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 := log.With(logger, "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 := log.With(logger, "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) { log.With(logger, "a", "b").Info("c", "d", "f") } +) diff --git a/log/tmfmt_logger.go b/log/tmfmt_logger.go new file mode 100644 index 000000000..db689c084 --- /dev/null +++ b/log/tmfmt_logger.go @@ -0,0 +1,116 @@ +package log + +import ( + "bytes" + "fmt" + "io" + "sync" + "time" + + kitlog "github.com/go-kit/kit/log" + "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] == level.Key() { + lvlIndex = i + switch keyvals[i+1].(type) { + case string: + lvl = keyvals[i+1].(string) + case level.Value: + lvl = keyvals[i+1].(level.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 +} diff --git a/log/tmfmt_logger_test.go b/log/tmfmt_logger_test.go new file mode 100644 index 000000000..ca5ee55ea --- /dev/null +++ b/log/tmfmt_logger_test.go @@ -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" } diff --git a/logger/log.go b/logger/log.go index 84f71c34f..2f4faef6b 100644 --- a/logger/log.go +++ b/logger/log.go @@ -1,10 +1,11 @@ +// DEPRECATED! Use newer log package. package logger import ( "os" - . "github.com/tendermint/tmlibs/common" "github.com/tendermint/log15" + . "github.com/tendermint/tmlibs/common" ) var mainHandler log15.Handler @@ -40,14 +41,14 @@ func MainHandler() log15.Handler { return mainHandler } -func BypassHandler() log15.Handler { - return bypassHandler -} - func New(ctx ...interface{}) log15.Logger { return NewMain(ctx...) } +func BypassHandler() log15.Handler { + return bypassHandler +} + func NewMain(ctx ...interface{}) log15.Logger { return log15.Root().New(ctx...) }