diff --git a/log/filter_test.go b/log/filter_test.go index e2766bd7e..529d87331 100644 --- a/log/filter_test.go +++ b/log/filter_test.go @@ -30,7 +30,7 @@ func TestFilterLevel(t *testing.T) { func TestFilterCaller(t *testing.T) { logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller) - log := NewFilter(NewFilter(logger)) + log := NewFilter(logger) log.Log(LevelDebug, "msg1", "te1st debug") logHelper := NewHelper(NewFilter(NewFilter(logger))) logHelper.Log(LevelDebug, "msg1", "te1st debug") @@ -65,4 +65,4 @@ func testFilterFunc(level Level, keyvals ...interface{}) bool { } } return false -} \ No newline at end of file +} diff --git a/log/value.go b/log/value.go index 6e3355b44..175f9d2d1 100644 --- a/log/value.go +++ b/log/value.go @@ -33,17 +33,16 @@ func Value(ctx context.Context, v interface{}) interface{} { func Caller(depth int) Valuer { return func(context.Context) interface{} { _, file, line, _ := runtime.Caller(depth) - h := strings.LastIndex(file, "/log/helper.go") - f := strings.LastIndex(file, "/log/filter.go") - if f > 0 { - _, file, line, _ = runtime.Caller(depth + 2) - if strings.LastIndex(file, "/log/helper.go") > 0 { - _, file, line, _ = runtime.Caller(depth + 3) + for true { + h := strings.LastIndex(file, "/log/helper.go") + f := strings.LastIndex(file, "/log/filter.go") + if h > 0 || f > 0 { + depth++ + _, file, line, _ = runtime.Caller(depth) + } else { + break } } - if h > 0 { - _, file, line, _ = runtime.Caller(depth + 1) - } idx := strings.LastIndexByte(file, '/') return file[idx+1:] + ":" + strconv.Itoa(line) }