log: fix global logger (#2034)

* log: fix global logger
pull/2035/head
Tony Chen 3 years ago committed by GitHub
parent 6eae1745bd
commit 79497dd3a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      log/global.go
  2. 5
      log/helper_test.go
  3. 19
      log/log.go
  4. 9
      log/log_test.go
  5. 3
      log/value.go

@ -38,7 +38,7 @@ func SetLogger(logger Logger) {
// GetLogger returns global logger appliance as logger in current process. // GetLogger returns global logger appliance as logger in current process.
func GetLogger() Logger { func GetLogger() Logger {
return global return global.GetLogger()
} }
// Log Print log by level and keyvals. // Log Print log by level and keyvals.

@ -73,7 +73,10 @@ func TestContext(t *testing.T) {
func Trace() Valuer { func Trace() Valuer {
return func(ctx context.Context) interface{} { return func(ctx context.Context) interface{} {
s := ctx.Value(traceKey{}).(string) s, ok := ctx.Value(traceKey{}).(string)
if !ok {
return nil
}
return s return s
} }
} }

@ -14,7 +14,7 @@ type Logger interface {
} }
type logger struct { type logger struct {
logs []Logger logger Logger
prefix []interface{} prefix []interface{}
hasValuer bool hasValuer bool
ctx context.Context ctx context.Context
@ -27,11 +27,9 @@ func (c *logger) Log(level Level, keyvals ...interface{}) error {
bindValues(c.ctx, kvs) bindValues(c.ctx, kvs)
} }
kvs = append(kvs, keyvals...) kvs = append(kvs, keyvals...)
for _, l := range c.logs { if err := c.logger.Log(level, kvs...); err != nil {
if err := l.Log(level, kvs...); err != nil {
return err return err
} }
}
return nil return nil
} }
@ -39,13 +37,13 @@ func (c *logger) Log(level Level, keyvals ...interface{}) error {
func With(l Logger, kv ...interface{}) Logger { func With(l Logger, kv ...interface{}) Logger {
c, ok := l.(*logger) c, ok := l.(*logger)
if !ok { if !ok {
return &logger{logs: []Logger{l}, prefix: kv, hasValuer: containsValuer(kv)} return &logger{logger: l, prefix: kv, hasValuer: containsValuer(kv), ctx: context.Background()}
} }
kvs := make([]interface{}, 0, len(c.prefix)+len(kv)) kvs := make([]interface{}, 0, len(c.prefix)+len(kv))
kvs = append(kvs, kv...) kvs = append(kvs, kv...)
kvs = append(kvs, c.prefix...) kvs = append(kvs, c.prefix...)
return &logger{ return &logger{
logs: c.logs, logger: l,
prefix: kvs, prefix: kvs,
hasValuer: containsValuer(kvs), hasValuer: containsValuer(kvs),
ctx: c.ctx, ctx: c.ctx,
@ -57,17 +55,12 @@ func With(l Logger, kv ...interface{}) Logger {
func WithContext(ctx context.Context, l Logger) Logger { func WithContext(ctx context.Context, l Logger) Logger {
c, ok := l.(*logger) c, ok := l.(*logger)
if !ok { if !ok {
return &logger{logs: []Logger{l}, ctx: ctx} return &logger{logger: l, ctx: ctx}
} }
return &logger{ return &logger{
logs: c.logs, logger: l,
prefix: c.prefix, prefix: c.prefix,
hasValuer: c.hasValuer, hasValuer: c.hasValuer,
ctx: ctx, ctx: ctx,
} }
} }
// MultiLogger wraps multi logger.
func MultiLogger(logs ...Logger) Logger {
return &logger{logs: logs}
}

@ -2,7 +2,6 @@ package log
import ( import (
"context" "context"
"os"
"testing" "testing"
) )
@ -12,14 +11,6 @@ func TestInfo(t *testing.T) {
_ = logger.Log(LevelInfo, "key1", "value1") _ = logger.Log(LevelInfo, "key1", "value1")
} }
func TestWrapper(t *testing.T) {
out := NewStdLogger(os.Stdout)
err := NewStdLogger(os.Stderr)
l := With(MultiLogger(out, err), "ts", DefaultTimestamp, "caller", DefaultCaller)
_ = l.Log(LevelInfo, "msg", "test")
}
func TestWithContext(t *testing.T) { func TestWithContext(t *testing.T) {
WithContext(context.Background(), nil) WithContext(context.Background(), nil)
} }

@ -9,9 +9,8 @@ import (
) )
var ( var (
defaultDepth = 3
// DefaultCaller is a Valuer that returns the file and line. // DefaultCaller is a Valuer that returns the file and line.
DefaultCaller = Caller(defaultDepth) DefaultCaller = Caller(3)
// DefaultTimestamp is a Valuer that returns the current wallclock time. // DefaultTimestamp is a Valuer that returns the current wallclock time.
DefaultTimestamp = Timestamp(time.RFC3339) DefaultTimestamp = Timestamp(time.RFC3339)

Loading…
Cancel
Save