diff --git a/contrib/log/aliyun/aliyun.go b/contrib/log/aliyun/aliyun.go index b45eed74b..46abd7e48 100644 --- a/contrib/log/aliyun/aliyun.go +++ b/contrib/log/aliyun/aliyun.go @@ -73,6 +73,10 @@ func WithAccessSecret(as string) Option { type Option func(alc *options) +func (a *aliyunLog) Close() error { + return a.producer.Close(5000) +} + func (a *aliyunLog) Log(level klog.Level, keyvals ...interface{}) error { buf := level.String() levelTitle := "level" diff --git a/contrib/log/zap/zap.go b/contrib/log/zap/zap.go index 8c0918065..411bd1c11 100644 --- a/contrib/log/zap/zap.go +++ b/contrib/log/zap/zap.go @@ -46,3 +46,7 @@ func (l *Logger) Log(level log.Level, keyvals ...interface{}) error { func (l *Logger) Sync() error { return l.log.Sync() } + +func (l *Logger) Close() error { + return l.log.Sync() +} diff --git a/log/global.go b/log/global.go index 6dbc722a8..f75b07e64 100644 --- a/log/global.go +++ b/log/global.go @@ -49,7 +49,7 @@ func Log(level Level, keyvals ...interface{}) { // Context with context logger. func Context(ctx context.Context) *Helper { - return NewHelper(WithContext(ctx, global)) + return NewHelper(WithContext(ctx, global.Logger)) } // Debug logs a message at debug level. diff --git a/log/log.go b/log/log.go index 264dd8c45..c1fad7703 100644 --- a/log/log.go +++ b/log/log.go @@ -43,7 +43,7 @@ func With(l Logger, kv ...interface{}) Logger { kvs = append(kvs, kv...) kvs = append(kvs, c.prefix...) return &logger{ - logger: l, + logger: c.logger, prefix: kvs, hasValuer: containsValuer(kvs), ctx: c.ctx, @@ -58,7 +58,7 @@ func WithContext(ctx context.Context, l Logger) Logger { return &logger{logger: l, ctx: ctx} } return &logger{ - logger: l, + logger: c.logger, prefix: c.prefix, hasValuer: c.hasValuer, ctx: ctx, diff --git a/log/std.go b/log/std.go index 8d79f5c53..6f2539816 100644 --- a/log/std.go +++ b/log/std.go @@ -45,3 +45,7 @@ func (l *stdLogger) Log(level Level, keyvals ...interface{}) error { l.pool.Put(buf) return nil } + +func (l *stdLogger) Close() error { + return nil +} diff --git a/log/value.go b/log/value.go index 36782f0dd..5aceffeb5 100644 --- a/log/value.go +++ b/log/value.go @@ -10,7 +10,7 @@ import ( var ( // DefaultCaller is a Valuer that returns the file and line. - DefaultCaller = Caller(3) + DefaultCaller = Caller(4) // DefaultTimestamp is a Valuer that returns the current wallclock time. DefaultTimestamp = Timestamp(time.RFC3339) @@ -30,20 +30,7 @@ func Value(ctx context.Context, v interface{}) interface{} { // Caller returns a Valuer that returns a pkg/file:line description of the caller. func Caller(depth int) Valuer { return func(context.Context) interface{} { - d := depth - _, file, line, _ := runtime.Caller(d) - if strings.LastIndex(file, "/log/filter.go") > 0 { - d++ - _, file, line, _ = runtime.Caller(d) - } - if strings.LastIndex(file, "/log/helper.go") > 0 { - d++ - _, file, line, _ = runtime.Caller(d) - } - if strings.LastIndex(file, "/log/global.go") > 0 { - d++ - _, file, line, _ = runtime.Caller(d) - } + _, file, line, _ := runtime.Caller(depth) idx := strings.LastIndexByte(file, '/') return file[idx+1:] + ":" + strconv.Itoa(line) }