Browse Source

Logger interface and tmLogger impl based on go-kit

pull/1842/head
Anton Kaliaev 8 years ago
parent
commit
ed76afd409
No known key found for this signature in database GPG Key ID: 7B6881D965918214
14 changed files with 472 additions and 67 deletions
  1. +1
    -2
      .gitignore
  2. +27
    -29
      common/service.go
  3. +1
    -1
      events/events.go
  4. +0
    -7
      events/log.go
  5. +20
    -20
      glide.lock
  6. +13
    -3
      glide.yaml
  7. +27
    -0
      log/logger.go
  8. +18
    -0
      log/nop_logger.go
  9. +18
    -0
      log/nop_logger_test.go
  10. +74
    -0
      log/tm_logger.go
  11. +41
    -0
      log/tm_logger_test.go
  12. +116
    -0
      log/tmfmt_logger.go
  13. +110
    -0
      log/tmfmt_logger_test.go
  14. +6
    -5
      logger/log.go

+ 1
- 2
.gitignore View File

@ -1,3 +1,2 @@
*.swp
*.swo
vendor vendor
.glide

+ 27
- 29
common/service.go View File

@ -3,7 +3,7 @@ package common
import ( import (
"sync/atomic" "sync/atomic"
"github.com/tendermint/log15"
"github.com/tendermint/tmlibs/log"
) )
type Service interface { type Service interface {
@ -19,6 +19,8 @@ type Service interface {
IsRunning() bool IsRunning() bool
String() string String() string
SetLogger(log.Logger)
} }
/* /*
@ -64,7 +66,7 @@ Typical usage:
} }
*/ */
type BaseService struct { type BaseService struct {
log log15.Logger
Logger log.Logger
name string name string
started uint32 // atomic started uint32 // atomic
stopped uint32 // atomic stopped uint32 // atomic
@ -74,27 +76,31 @@ type BaseService struct {
impl Service 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{ 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 // Implements Servce
func (bs *BaseService) Start() (bool, error) { func (bs *BaseService) Start() (bool, error) {
if atomic.CompareAndSwapUint32(&bs.started, 0, 1) { if atomic.CompareAndSwapUint32(&bs.started, 0, 1) {
if atomic.LoadUint32(&bs.stopped) == 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 return false, nil
} else { } 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() err := bs.impl.OnStart()
if err != nil { if err != nil {
@ -104,9 +110,7 @@ func (bs *BaseService) Start() (bool, error) {
} }
return true, err return true, err
} else { } 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 return false, nil
} }
} }
@ -119,16 +123,12 @@ func (bs *BaseService) OnStart() error { return nil }
// Implements Service // Implements Service
func (bs *BaseService) Stop() bool { func (bs *BaseService) Stop() bool {
if atomic.CompareAndSwapUint32(&bs.stopped, 0, 1) { 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() bs.impl.OnStop()
close(bs.Quit) close(bs.Quit)
return true return true
} else { } 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 return false
} }
} }
@ -147,9 +147,7 @@ func (bs *BaseService) Reset() (bool, error) {
bs.Quit = make(chan struct{}) bs.Quit = make(chan struct{})
return true, bs.impl.OnReset() return true, bs.impl.OnReset()
} else { } 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 return false, nil
} }
// never happens // never happens
@ -182,11 +180,11 @@ type QuitService struct {
BaseService 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{ return &QuitService{
BaseService: *NewBaseService(log, name, impl),
BaseService: *NewBaseService(logger, name, impl),
} }
} }

+ 1
- 1
events/events.go View File

@ -45,7 +45,7 @@ type eventSwitch struct {
func NewEventSwitch() EventSwitch { func NewEventSwitch() EventSwitch {
evsw := &eventSwitch{} evsw := &eventSwitch{}
evsw.BaseService = *NewBaseService(log, "EventSwitch", evsw)
evsw.BaseService = *NewBaseService(nil, "EventSwitch", evsw)
return evsw return evsw
} }


+ 0
- 7
events/log.go View File

@ -1,7 +0,0 @@
package events
import (
"github.com/tendermint/tmlibs/logger"
)
var log = logger.New("module", "events")

+ 20
- 20
glide.lock View File

@ -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: imports:
- name: github.com/fsnotify/fsnotify - name: github.com/fsnotify/fsnotify
version: 4da3e2cfbabc9f751898f250b49f2439785783a1 version: 4da3e2cfbabc9f751898f250b49f2439785783a1
@ -12,11 +12,11 @@ imports:
- name: github.com/go-logfmt/logfmt - name: github.com/go-logfmt/logfmt
version: 390ab7935ee28ec6b286364bba9b4dd6410cb3d5 version: 390ab7935ee28ec6b286364bba9b4dd6410cb3d5
- name: github.com/go-stack/stack - name: github.com/go-stack/stack
version: 100eb0c0a9c5b306ca2fb4f165df21d80ada4b82
version: 7a2f19628aabfe68f0766b59e74d6315f8347d22
- name: github.com/golang/snappy - name: github.com/golang/snappy
version: 553a641470496b2327abcac10b36396bd98e45c9 version: 553a641470496b2327abcac10b36396bd98e45c9
- name: github.com/hashicorp/hcl - name: github.com/hashicorp/hcl
version: 630949a3c5fa3c613328e1b8256052cbc2327c9b
version: a4b07c25de5ff55ad3b8936cea69a79a3d95a855
subpackages: subpackages:
- hcl/ast - hcl/ast
- hcl/parser - hcl/parser
@ -35,17 +35,17 @@ imports:
- name: github.com/magiconair/properties - name: github.com/magiconair/properties
version: 51463bfca2576e06c62a8504b5c0f06d61312647 version: 51463bfca2576e06c62a8504b5c0f06d61312647
- name: github.com/mattn/go-colorable - name: github.com/mattn/go-colorable
version: d228849504861217f796da67fae4f6e347643f15
version: ded68f7a9561c023e790de24279db7ebf473ea80
- name: github.com/mattn/go-isatty - name: github.com/mattn/go-isatty
version: 30a891c33c7cde7b02a981314b4228ec99380cca
version: fc9e8d8ef48496124e79ae0df75490096eccf6fe
- name: github.com/mitchellh/mapstructure - name: github.com/mitchellh/mapstructure
version: 53818660ed4955e899c0bcafa97299a388bd7c8e
version: cc8532a8e9a55ea36402aa21efdf403a60d34096
- name: github.com/pelletier/go-buffruneio - name: github.com/pelletier/go-buffruneio
version: c37440a7cf42ac63b919c752ca73a85067e05992 version: c37440a7cf42ac63b919c752ca73a85067e05992
- name: github.com/pelletier/go-toml - name: github.com/pelletier/go-toml
version: 13d49d4606eb801b8f01ae542b4afc4c6ee3d84a
version: 97253b98df84f9eef872866d079e74b8265150f1
- name: github.com/pkg/errors - name: github.com/pkg/errors
version: bfd5150e4e41705ded2129ec33379de1cb90b513
version: c605e284fe17294bda444b34710735b29d1a9d90
- name: github.com/spf13/afero - name: github.com/spf13/afero
version: 9be650865eab0c12963d8753212f4f9c66cdcf12 version: 9be650865eab0c12963d8753212f4f9c66cdcf12
subpackages: subpackages:
@ -53,15 +53,15 @@ imports:
- name: github.com/spf13/cast - name: github.com/spf13/cast
version: acbeb36b902d72a7a4c18e8f3241075e7ab763e4 version: acbeb36b902d72a7a4c18e8f3241075e7ab763e4
- name: github.com/spf13/cobra - name: github.com/spf13/cobra
version: fcd0c5a1df88f5d6784cb4feead962c3f3d0b66c
version: db6b9a8b3f3f400c8ecb4a4d7d02245b8facad66
- name: github.com/spf13/jwalterweatherman - name: github.com/spf13/jwalterweatherman
version: fa7ca7e836cf3a8bb4ebf799f472c12d7e903d66 version: fa7ca7e836cf3a8bb4ebf799f472c12d7e903d66
- name: github.com/spf13/pflag - name: github.com/spf13/pflag
version: 9ff6c6923cfffbcd502984b8e0c80539a94968b7
version: 80fe0fb4eba54167e2ccae1c6c950e72abf61b73
- name: github.com/spf13/viper - name: github.com/spf13/viper
version: 5d46e70da8c0b6f812e0b170b7a985753b5c63cb
version: 0967fc9aceab2ce9da34061253ac10fb99bba5b2
- name: github.com/syndtr/goleveldb - name: github.com/syndtr/goleveldb
version: 23851d93a2292dcc56e71a18ec9e0624d84a0f65
version: 8c81ea47d4c41a385645e133e15510fc6a2a74b4
subpackages: subpackages:
- leveldb - leveldb
- leveldb/cache - leveldb/cache
@ -76,24 +76,24 @@ imports:
- leveldb/table - leveldb/table
- leveldb/util - leveldb/util
- name: github.com/tendermint/go-wire - name: github.com/tendermint/go-wire
version: 334005c236d19c632fb5f073f9de3b0fab6a522b
version: b53add0b622662731985485f3a19be7f684660b8
subpackages: subpackages:
- data - data
- data/base58 - data/base58
- name: github.com/tendermint/log15 - name: github.com/tendermint/log15
version: ae0f3d6450da9eac7074b439c8e1c3cabf0d5ce6
version: f91285dece9f4875421b481da3e613d83d44f29b
subpackages: subpackages:
- term - term
- name: golang.org/x/crypto - name: golang.org/x/crypto
version: 7c6cc321c680f03b9ef0764448e780704f486b51
version: 5a033cc77e57eca05bdb50522851d29e03569cbe
subpackages: subpackages:
- ripemd160 - ripemd160
- name: golang.org/x/sys - name: golang.org/x/sys
version: d75a52659825e75fff6158388dddc6a5b04f9ba5
version: 9ccfe848b9db8435a24c424abbc07a921adf1df5
subpackages: subpackages:
- unix - unix
- name: golang.org/x/text - name: golang.org/x/text
version: f4b4367115ec2de254587813edaa901bc1c723a8
version: 470f45bf29f4147d6fbd7dfd0a02a848e49f5bf4
subpackages: subpackages:
- transform - transform
- unicode/norm - unicode/norm
@ -101,7 +101,7 @@ imports:
version: cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b version: cd8b52f8269e0feb286dfeef29f8fe4d5b397e0b
testImports: testImports:
- name: github.com/davecgh/go-spew - name: github.com/davecgh/go-spew
version: 6d212800a42e8ab5c146b8ace3490ee17e5225f9
version: 04cdfd42973bb9c8589fd6a731800cf222fde1a9
subpackages: subpackages:
- spew - spew
- name: github.com/pmezard/go-difflib - name: github.com/pmezard/go-difflib
@ -109,7 +109,7 @@ testImports:
subpackages: subpackages:
- difflib - difflib
- name: github.com/stretchr/testify - name: github.com/stretchr/testify
version: 69483b4bd14f5845b5a1e55bca19e954e827f1d0
version: 4d4bfba8f1d1027c4fdbe371823030df51419987
subpackages: subpackages:
- assert - assert
- require - require

+ 13
- 3
glide.yaml View File

@ -1,20 +1,30 @@
package: github.com/tendermint/tmlibs package: github.com/tendermint/tmlibs
import: 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/jmhodges/levigo
- package: github.com/pkg/errors
- package: github.com/spf13/cobra
- package: github.com/spf13/viper
- package: github.com/syndtr/goleveldb - package: github.com/syndtr/goleveldb
subpackages: subpackages:
- leveldb - leveldb
- leveldb/errors - leveldb/errors
- leveldb/opt - leveldb/opt
- package: github.com/tendermint/go-wire - package: github.com/tendermint/go-wire
subpackages:
- data
- data/base58
- package: github.com/tendermint/log15 - package: github.com/tendermint/log15
- package: golang.org/x/crypto - package: golang.org/x/crypto
subpackages: subpackages:
- ripemd160 - ripemd160
- package: github.com/go-logfmt/logfmt
- package: github.com/spf13/cobra
- package: github.com/spf13/viper
testImport: testImport:
- package: github.com/stretchr/testify - package: github.com/stretchr/testify
subpackages: subpackages:
- assert - assert
- require

+ 27
- 0
log/logger.go View File

@ -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))
}
}

+ 18
- 0
log/nop_logger.go View File

@ -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
}

+ 18
- 0
log/nop_logger_test.go View File

@ -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)
}
}

+ 74
- 0
log/tm_logger.go View File

@ -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...)
}

+ 41
- 0
log/tm_logger_test.go View File

@ -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") }
)

+ 116
- 0
log/tmfmt_logger.go View File

@ -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
}

+ 110
- 0
log/tmfmt_logger_test.go View File

@ -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" }

+ 6
- 5
logger/log.go View File

@ -1,10 +1,11 @@
// DEPRECATED! Use newer log package.
package logger package logger
import ( import (
"os" "os"
. "github.com/tendermint/tmlibs/common"
"github.com/tendermint/log15" "github.com/tendermint/log15"
. "github.com/tendermint/tmlibs/common"
) )
var mainHandler log15.Handler var mainHandler log15.Handler
@ -40,14 +41,14 @@ func MainHandler() log15.Handler {
return mainHandler return mainHandler
} }
func BypassHandler() log15.Handler {
return bypassHandler
}
func New(ctx ...interface{}) log15.Logger { func New(ctx ...interface{}) log15.Logger {
return NewMain(ctx...) return NewMain(ctx...)
} }
func BypassHandler() log15.Handler {
return bypassHandler
}
func NewMain(ctx ...interface{}) log15.Logger { func NewMain(ctx ...interface{}) log15.Logger {
return log15.Root().New(ctx...) return log15.Root().New(ctx...)
} }


Loading…
Cancel
Save