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. 14
      log/filter.go
  2. 4
      log/helper_writer.go
  3. 40
      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,9 +19,7 @@ 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
}
} }
} }

@ -37,32 +37,34 @@ 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)
kvs := make([]interface{}, 0, len(c.prefix)+len(kv)) if !ok {
kvs = append(kvs, kv...) return &logger{logs: []Logger{l}, prefix: kv, hasValuer: containsValuer(kv)}
kvs = append(kvs, c.prefix...) }
return &logger{ kvs := make([]interface{}, 0, len(c.prefix)+len(kv))
logs: c.logs, kvs = append(kvs, kv...)
prefix: kvs, kvs = append(kvs, c.prefix...)
hasValuer: containsValuer(kvs), return &logger{
ctx: c.ctx, 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 // 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)
return &logger{ if !ok {
logs: c.logs, return &logger{logs: []Logger{l}, ctx: ctx}
prefix: c.prefix, }
hasValuer: c.hasValuer, return &logger{
ctx: ctx, logs: c.logs,
} prefix: c.prefix,
hasValuer: c.hasValuer,
ctx: ctx,
} }
return &logger{logs: []Logger{l}, ctx: ctx}
} }
// MultiLogger wraps multi logger. // MultiLogger wraps multi logger.

Loading…
Cancel
Save