refactor(log): 优化调用栈查找逻辑以提高准确性
- 注释掉批量缓存操作测试的调用,避免不必要的执行 - 改进 findCaller 函数,增加对 application.go 的优先记录逻辑 - 确保返回的调用者信息优先考虑应用层代码,提升日志信息的准确性 - 增强框架文件的过滤机制,确保更清晰的调用栈信息
This commit is contained in:
parent
3d83c41905
commit
3fd0975427
@ -99,7 +99,7 @@ func main() {
|
||||
"cache-compat": func(that *Context) { that.Display(0, testCacheCompatible(that)) },
|
||||
// 批量缓存操作测试
|
||||
"cache-batch": func(that *Context) {
|
||||
TestBatchCacheOperations(that.Application)
|
||||
//TestBatchCacheOperations(that.Application)
|
||||
that.Display(0, Map{"message": "批量缓存测试完成,请查看控制台输出和日志文件"})
|
||||
},
|
||||
},
|
||||
|
||||
@ -141,8 +141,13 @@ func isHoTimeFrameworkFile(file string) bool {
|
||||
// 对caller进行递归查询, 直到找到非框架层产生的第一个调用.
|
||||
// 遍历调用栈,跳过框架层文件,找到应用层代码
|
||||
// 使用层数限制确保不会误过滤应用层同名目录
|
||||
// 返回优先级:应用层代码 > application.go > 其他框架文件
|
||||
func findCaller(skip int) string {
|
||||
frameworkCount := 0 // 连续框架层计数
|
||||
var lastFrameworkFile string
|
||||
var lastFrameworkLine int
|
||||
var applicationFile string // 优先记录 application.go 位置
|
||||
var applicationLine int
|
||||
|
||||
// 遍历调用栈,找到第一个非框架文件
|
||||
for i := 0; i < 20; i++ {
|
||||
@ -151,8 +156,17 @@ func findCaller(skip int) string {
|
||||
break
|
||||
}
|
||||
|
||||
if isHoTimeFrameworkFile(file) {
|
||||
isFramework := isHoTimeFrameworkFile(file)
|
||||
|
||||
if isFramework {
|
||||
frameworkCount++
|
||||
lastFrameworkFile = file
|
||||
lastFrameworkLine = line
|
||||
// 优先记录 application.go 位置(HoTime 框架入口)
|
||||
if strings.Contains(file, "application.go") {
|
||||
applicationFile = file
|
||||
applicationLine = line
|
||||
}
|
||||
// 层数限制:如果已经跳过太多层,停止跳过
|
||||
if frameworkCount >= maxFrameworkDepth {
|
||||
return fmt.Sprintf("%s:%d", file, line)
|
||||
@ -164,7 +178,18 @@ func findCaller(skip int) string {
|
||||
return fmt.Sprintf("%s:%d", file, line)
|
||||
}
|
||||
|
||||
// 如果找不到应用层,返回最初的调用者
|
||||
// 如果找不到应用层,返回最后记录的框架文件位置
|
||||
// 优先级:application.go > 其他框架文件 > 第一个调用者
|
||||
// 确保不会返回 logrus 或 runtime 等三方组件位置
|
||||
if applicationFile != "" {
|
||||
return fmt.Sprintf("%s:%d", applicationFile, applicationLine)
|
||||
}
|
||||
|
||||
if lastFrameworkFile != "" {
|
||||
return fmt.Sprintf("%s:%d", lastFrameworkFile, lastFrameworkLine)
|
||||
}
|
||||
|
||||
// 最后的回退:返回第一个调用者
|
||||
file, line := getCaller(skip)
|
||||
return fmt.Sprintf("%s:%d", file, line)
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user