From 2eb615b38ad87e8e24f2237132fbf95b15693516 Mon Sep 17 00:00:00 2001 From: songzhibin97 <49082129+songzhibin97@users.noreply.github.com> Date: Sat, 21 May 2022 16:23:09 +0800 Subject: [PATCH] style:log (#2014) --- log/filter.go | 14 +++++++------- log/helper_writer.go | 4 +--- log/log.go | 40 +++++++++++++++++++++------------------- 3 files changed, 29 insertions(+), 29 deletions(-) diff --git a/log/filter.go b/log/filter.go index 9ef87ddf1..ab5723770 100644 --- a/log/filter.go +++ b/log/filter.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 var fkv []interface{} - if l, ok := f.logger.(*logger); ok { - if len(l.prefix) > 0 { - fkv = make([]interface{}, 0, len(l.prefix)+len(keyvals)) - fkv = append(fkv, l.prefix...) - fkv = append(fkv, keyvals...) - } - } else { + l, ok := f.logger.(*logger) + if ok && len(l.prefix) > 0 { + fkv = make([]interface{}, 0, len(l.prefix)+len(keyvals)) + fkv = append(fkv, l.prefix...) + fkv = append(fkv, keyvals...) + } + if !ok { fkv = keyvals } if f.filter != nil && f.filter(level, fkv...) { diff --git a/log/helper_writer.go b/log/helper_writer.go index 0c76a7bae..5ab45802d 100644 --- a/log/helper_writer.go +++ b/log/helper_writer.go @@ -19,9 +19,7 @@ func WithWriterLevel(level Level) WriterOptionFn { // WithWriteMessageKey set writerWrapper helper message key. func WithWriteMessageKey(key string) WriterOptionFn { return func(w *writerWrapper) { - if key != "" { - w.helper.msgKey = key - } + w.helper.msgKey = key } } diff --git a/log/log.go b/log/log.go index 1b612f8b8..2a290b707 100644 --- a/log/log.go +++ b/log/log.go @@ -37,32 +37,34 @@ func (c *logger) Log(level Level, keyvals ...interface{}) error { // With with logger fields. func With(l Logger, kv ...interface{}) Logger { - if c, ok := l.(*logger); ok { - kvs := make([]interface{}, 0, len(c.prefix)+len(kv)) - kvs = append(kvs, kv...) - kvs = append(kvs, c.prefix...) - return &logger{ - logs: c.logs, - prefix: kvs, - hasValuer: containsValuer(kvs), - ctx: c.ctx, - } + 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 = append(kvs, kv...) + kvs = append(kvs, c.prefix...) + return &logger{ + logs: c.logs, + prefix: kvs, + hasValuer: containsValuer(kvs), + ctx: c.ctx, } - return &logger{logs: []Logger{l}, prefix: kv, hasValuer: containsValuer(kv)} } // WithContext returns a shallow copy of l with its context changed // to ctx. The provided ctx must be non-nil. func WithContext(ctx context.Context, l Logger) Logger { - if c, ok := l.(*logger); ok { - return &logger{ - logs: c.logs, - prefix: c.prefix, - hasValuer: c.hasValuer, - ctx: ctx, - } + c, ok := l.(*logger) + if !ok { + return &logger{logs: []Logger{l}, ctx: ctx} + } + return &logger{ + logs: c.logs, + prefix: c.prefix, + hasValuer: c.hasValuer, + ctx: ctx, } - return &logger{logs: []Logger{l}, ctx: ctx} } // MultiLogger wraps multi logger.