modify log caller

pull/1053/head
包子 4 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) {
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
}
}

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

Loading…
Cancel
Save