pull/2021/head
songzhibin97 3 years ago committed by GitHub
parent 5e0744a47b
commit 2eb615b38a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      log/filter.go
  2. 2
      log/helper_writer.go
  3. 14
      log/log.go

@ -66,13 +66,13 @@ func (f *Filter) Log(level Level, keyvals ...interface{}) error {
} }
// fkv is used to provide a slice to contains both logger.prefix and keyvals for filter // fkv is used to provide a slice to contains both logger.prefix and keyvals for filter
var fkv []interface{} var fkv []interface{}
if l, ok := f.logger.(*logger); ok { l, ok := f.logger.(*logger)
if len(l.prefix) > 0 { if ok && len(l.prefix) > 0 {
fkv = make([]interface{}, 0, len(l.prefix)+len(keyvals)) fkv = make([]interface{}, 0, len(l.prefix)+len(keyvals))
fkv = append(fkv, l.prefix...) fkv = append(fkv, l.prefix...)
fkv = append(fkv, keyvals...) fkv = append(fkv, keyvals...)
} }
} else { if !ok {
fkv = keyvals fkv = keyvals
} }
if f.filter != nil && f.filter(level, fkv...) { if f.filter != nil && f.filter(level, fkv...) {

@ -19,11 +19,9 @@ func WithWriterLevel(level Level) WriterOptionFn {
// WithWriteMessageKey set writerWrapper helper message key. // WithWriteMessageKey set writerWrapper helper message key.
func WithWriteMessageKey(key string) WriterOptionFn { func WithWriteMessageKey(key string) WriterOptionFn {
return func(w *writerWrapper) { return func(w *writerWrapper) {
if key != "" {
w.helper.msgKey = key w.helper.msgKey = key
} }
} }
}
// NewWriter return a writer wrapper. // NewWriter return a writer wrapper.
func NewWriter(logger Logger, opts ...WriterOptionFn) io.Writer { func NewWriter(logger Logger, opts ...WriterOptionFn) io.Writer {

@ -37,7 +37,10 @@ func (c *logger) Log(level Level, keyvals ...interface{}) error {
// With with logger fields. // With with logger fields.
func With(l Logger, kv ...interface{}) Logger { func With(l Logger, kv ...interface{}) Logger {
if c, ok := l.(*logger); ok { c, ok := l.(*logger)
if !ok {
return &logger{logs: []Logger{l}, prefix: kv, hasValuer: containsValuer(kv)}
}
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...)
@ -48,13 +51,14 @@ func With(l Logger, kv ...interface{}) Logger {
ctx: c.ctx, ctx: c.ctx,
} }
} }
return &logger{logs: []Logger{l}, prefix: kv, hasValuer: containsValuer(kv)}
}
// WithContext returns a shallow copy of l with its context changed // WithContext returns a shallow copy of l with its context changed
// to ctx. The provided ctx must be non-nil. // to ctx. The provided ctx must be non-nil.
func WithContext(ctx context.Context, l Logger) Logger { func WithContext(ctx context.Context, l Logger) Logger {
if c, ok := l.(*logger); ok { c, ok := l.(*logger)
if !ok {
return &logger{logs: []Logger{l}, ctx: ctx}
}
return &logger{ return &logger{
logs: c.logs, logs: c.logs,
prefix: c.prefix, prefix: c.prefix,
@ -62,8 +66,6 @@ func WithContext(ctx context.Context, l Logger) Logger {
ctx: ctx, ctx: ctx,
} }
} }
return &logger{logs: []Logger{l}, ctx: ctx}
}
// MultiLogger wraps multi logger. // MultiLogger wraps multi logger.
func MultiLogger(logs ...Logger) Logger { func MultiLogger(logs ...Logger) Logger {

Loading…
Cancel
Save