🩹Fix incorrect log depth when use log.WithContext (#2666)

fix withContext caller
pull/2675/head
Jiun Lee 2023-10-08 19:45:11 +08:00 committed by GitHub
parent 6ecd607d97
commit fc2ab3387a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -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) {

View File

@ -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),