You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.2 KiB
65 lines
1.2 KiB
package log |
|
|
|
import ( |
|
"context" |
|
"log" |
|
) |
|
|
|
var DefaultLogger = NewStdLogger(log.Writer()) |
|
|
|
type Logger interface { |
|
Log(level Level, keyvals ...any) error |
|
} |
|
|
|
type logger struct { |
|
logger Logger |
|
prefix []any |
|
hasValuer bool |
|
ctx context.Context |
|
} |
|
|
|
func (l *logger) Log(level Level, keyvals ...any) error { |
|
kvs := make([]any, 0, len(l.prefix)+len(keyvals)) |
|
kvs = append(kvs, l.prefix...) |
|
if l.hasValuer { |
|
bindValues(l.ctx, kvs) |
|
} |
|
kvs = append(kvs, keyvals...) |
|
return l.logger.Log(level, kvs...) |
|
} |
|
|
|
func With(l Logger, kv ...any) Logger { |
|
c, ok := l.(*logger) |
|
if !ok { |
|
return &logger{ |
|
logger: l, |
|
prefix: kv, |
|
hasValuer: containsValuer(kv), |
|
ctx: context.Background(), |
|
} |
|
} |
|
kvs := make([]any, 0, len(c.prefix)+len(kv)) |
|
kvs = append(kvs, c.prefix...) |
|
kvs = append(kvs, kv...) |
|
return &logger{ |
|
logger: c.logger, |
|
prefix: kvs, |
|
hasValuer: containsValuer(kvs), |
|
ctx: c.ctx, |
|
} |
|
} |
|
func WithContext(ctx context.Context, l Logger) Logger { |
|
c, ok := l.(*logger) |
|
if !ok { |
|
return &logger{ |
|
logger: l, |
|
ctx: ctx, |
|
} |
|
} |
|
return &logger{ |
|
logger: c.logger, |
|
prefix: c.prefix, |
|
hasValuer: c.hasValuer, |
|
ctx: ctx, |
|
} |
|
}
|
|
|