From fc2ab3387ade433b1f17bb3459eeb2a89483073f Mon Sep 17 00:00:00 2001 From: Jiun Lee Date: Sun, 8 Oct 2023 19:45:11 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=A9=B9Fix=20incorrect=20log=20depth=20whe?= =?UTF-8?q?n=20use=20log.WithContext=20(#2666)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix withContext caller --- log/default.go | 6 +++++- log/default_test.go | 16 ++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/log/default.go b/log/default.go index e78f1e3d..c898cd6c 100644 --- a/log/default.go +++ b/log/default.go @@ -188,7 +188,11 @@ func (l *defaultLogger) Panicw(msg string, keysAndValues ...interface{}) { } func (l *defaultLogger) WithContext(_ context.Context) CommonLogger { - return l + return &defaultLogger{ + stdlog: l.stdlog, + level: l.level, + depth: l.depth - 1, + } } func (l *defaultLogger) SetLevel(level Level) { diff --git a/log/default_test.go b/log/default_test.go index 7562b0a1..9dd5fd7c 100644 --- a/log/default_test.go +++ b/log/default_test.go @@ -156,6 +156,22 @@ func Test_LogfKeyAndValues(t *testing.T) { } } +func Test_WithContextCaller(t *testing.T) { + logger = &defaultLogger{ + stdlog: log.New(os.Stderr, "", log.Lshortfile), + depth: 4, + } + + var w byteSliceWriter + SetOutput(&w) + ctx := context.TODO() + + WithContext(ctx).Info("") + Info("") + + utils.AssertEqual(t, "default_test.go:169: [Info] \ndefault_test.go:170: [Info] \n", string(w.b)) +} + func Test_SetLevel(t *testing.T) { setLogger := &defaultLogger{ stdlog: log.New(os.Stderr, "", log.LstdFlags|log.Lshortfile|log.Lmicroseconds),