modify log caller

pull/1053/head
包子 3 years ago
parent 38673521e4
commit 7bebc58381
  1. 4
      log/filter_test.go
  2. 17
      log/value.go

@ -30,7 +30,7 @@ func TestFilterLevel(t *testing.T) {
func TestFilterCaller(t *testing.T) { func TestFilterCaller(t *testing.T) {
logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller) logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller)
log := NewFilter(NewFilter(logger)) log := NewFilter(logger)
log.Log(LevelDebug, "msg1", "te1st debug") log.Log(LevelDebug, "msg1", "te1st debug")
logHelper := NewHelper(NewFilter(NewFilter(logger))) logHelper := NewHelper(NewFilter(NewFilter(logger)))
logHelper.Log(LevelDebug, "msg1", "te1st debug") logHelper.Log(LevelDebug, "msg1", "te1st debug")
@ -65,4 +65,4 @@ func testFilterFunc(level Level, keyvals ...interface{}) bool {
} }
} }
return false return false
} }

@ -33,17 +33,16 @@ func Value(ctx context.Context, v interface{}) interface{} {
func Caller(depth int) Valuer { func Caller(depth int) Valuer {
return func(context.Context) interface{} { return func(context.Context) interface{} {
_, file, line, _ := runtime.Caller(depth) _, file, line, _ := runtime.Caller(depth)
h := strings.LastIndex(file, "/log/helper.go") for true {
f := strings.LastIndex(file, "/log/filter.go") h := strings.LastIndex(file, "/log/helper.go")
if f > 0 { f := strings.LastIndex(file, "/log/filter.go")
_, file, line, _ = runtime.Caller(depth + 2) if h > 0 || f > 0 {
if strings.LastIndex(file, "/log/helper.go") > 0 { depth++
_, file, line, _ = runtime.Caller(depth + 3) _, file, line, _ = runtime.Caller(depth)
} else {
break
} }
} }
if h > 0 {
_, file, line, _ = runtime.Caller(depth + 1)
}
idx := strings.LastIndexByte(file, '/') idx := strings.LastIndexByte(file, '/')
return file[idx+1:] + ":" + strconv.Itoa(line) return file[idx+1:] + ":" + strconv.Itoa(line)
} }

Loading…
Cancel
Save