mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
parent
6eae1745bd
commit
79497dd3a5
@ -38,7 +38,7 @@ func SetLogger(logger Logger) {
|
||||
|
||||
// GetLogger returns global logger appliance as logger in current process.
|
||||
func GetLogger() Logger {
|
||||
return global
|
||||
return global.GetLogger()
|
||||
}
|
||||
|
||||
// Log Print log by level and keyvals.
|
||||
|
@ -73,7 +73,10 @@ func TestContext(t *testing.T) {
|
||||
|
||||
func Trace() Valuer {
|
||||
return func(ctx context.Context) interface{} {
|
||||
s := ctx.Value(traceKey{}).(string)
|
||||
s, ok := ctx.Value(traceKey{}).(string)
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
21
log/log.go
21
log/log.go
@ -14,7 +14,7 @@ type Logger interface {
|
||||
}
|
||||
|
||||
type logger struct {
|
||||
logs []Logger
|
||||
logger Logger
|
||||
prefix []interface{}
|
||||
hasValuer bool
|
||||
ctx context.Context
|
||||
@ -27,10 +27,8 @@ func (c *logger) Log(level Level, keyvals ...interface{}) error {
|
||||
bindValues(c.ctx, kvs)
|
||||
}
|
||||
kvs = append(kvs, keyvals...)
|
||||
for _, l := range c.logs {
|
||||
if err := l.Log(level, kvs...); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.logger.Log(level, kvs...); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -39,13 +37,13 @@ func (c *logger) Log(level Level, keyvals ...interface{}) error {
|
||||
func With(l Logger, kv ...interface{}) Logger {
|
||||
c, ok := l.(*logger)
|
||||
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 = append(kvs, kv...)
|
||||
kvs = append(kvs, c.prefix...)
|
||||
return &logger{
|
||||
logs: c.logs,
|
||||
logger: l,
|
||||
prefix: kvs,
|
||||
hasValuer: containsValuer(kvs),
|
||||
ctx: c.ctx,
|
||||
@ -57,17 +55,12 @@ func With(l Logger, kv ...interface{}) Logger {
|
||||
func WithContext(ctx context.Context, l Logger) Logger {
|
||||
c, ok := l.(*logger)
|
||||
if !ok {
|
||||
return &logger{logs: []Logger{l}, ctx: ctx}
|
||||
return &logger{logger: l, ctx: ctx}
|
||||
}
|
||||
return &logger{
|
||||
logs: c.logs,
|
||||
logger: l,
|
||||
prefix: c.prefix,
|
||||
hasValuer: c.hasValuer,
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
// MultiLogger wraps multi logger.
|
||||
func MultiLogger(logs ...Logger) Logger {
|
||||
return &logger{logs: logs}
|
||||
}
|
||||
|
@ -2,7 +2,6 @@ package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@ -12,14 +11,6 @@ func TestInfo(t *testing.T) {
|
||||
_ = 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) {
|
||||
WithContext(context.Background(), nil)
|
||||
}
|
||||
|
@ -9,9 +9,8 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
defaultDepth = 3
|
||||
// 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 = Timestamp(time.RFC3339)
|
||||
|
Loading…
x
Reference in New Issue
Block a user