Commit d87d51b7013134ead568ea3ef9119adaea0384a2
1 parent
2201cd6a
【功能优化】全局:ApiAccessLogInterceptor 额外打印 Controller 地址,基于 https://t.zsxq.com/Z70Sl 建议
Showing
1 changed file
with
36 additions
and
0 deletions
yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/apilog/core/interceptor/ApiAccessLogInterceptor.java
| 1 | package cn.iocoder.yudao.framework.apilog.core.interceptor; | 1 | package cn.iocoder.yudao.framework.apilog.core.interceptor; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.collection.CollUtil; | 3 | import cn.hutool.core.collection.CollUtil; |
| 4 | +import cn.hutool.core.io.FileUtil; | ||
| 5 | +import cn.hutool.core.io.resource.ResourceUtil; | ||
| 4 | import cn.hutool.core.util.StrUtil; | 6 | import cn.hutool.core.util.StrUtil; |
| 5 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; | 7 | import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; |
| 6 | import cn.iocoder.yudao.framework.common.util.spring.SpringUtils; | 8 | import cn.iocoder.yudao.framework.common.util.spring.SpringUtils; |
| @@ -11,7 +13,11 @@ import org.springframework.util.StopWatch; | @@ -11,7 +13,11 @@ import org.springframework.util.StopWatch; | ||
| 11 | import org.springframework.web.method.HandlerMethod; | 13 | import org.springframework.web.method.HandlerMethod; |
| 12 | import org.springframework.web.servlet.HandlerInterceptor; | 14 | import org.springframework.web.servlet.HandlerInterceptor; |
| 13 | 15 | ||
| 16 | +import java.lang.reflect.Method; | ||
| 17 | +import java.util.List; | ||
| 14 | import java.util.Map; | 18 | import java.util.Map; |
| 19 | +import java.util.Optional; | ||
| 20 | +import java.util.stream.IntStream; | ||
| 15 | 21 | ||
| 16 | /** | 22 | /** |
| 17 | * API 访问日志 Interceptor | 23 | * API 访问日志 Interceptor |
| @@ -49,6 +55,8 @@ public class ApiAccessLogInterceptor implements HandlerInterceptor { | @@ -49,6 +55,8 @@ public class ApiAccessLogInterceptor implements HandlerInterceptor { | ||
| 49 | StopWatch stopWatch = new StopWatch(); | 55 | StopWatch stopWatch = new StopWatch(); |
| 50 | stopWatch.start(); | 56 | stopWatch.start(); |
| 51 | request.setAttribute(ATTRIBUTE_STOP_WATCH, stopWatch); | 57 | request.setAttribute(ATTRIBUTE_STOP_WATCH, stopWatch); |
| 58 | + // 打印 Controller 路径 | ||
| 59 | + printHandlerMethodPosition(handlerMethod); | ||
| 52 | } | 60 | } |
| 53 | return true; | 61 | return true; |
| 54 | } | 62 | } |
| @@ -64,4 +72,32 @@ public class ApiAccessLogInterceptor implements HandlerInterceptor { | @@ -64,4 +72,32 @@ public class ApiAccessLogInterceptor implements HandlerInterceptor { | ||
| 64 | } | 72 | } |
| 65 | } | 73 | } |
| 66 | 74 | ||
| 75 | + /** | ||
| 76 | + * 打印 Controller 方法路径 | ||
| 77 | + */ | ||
| 78 | + private void printHandlerMethodPosition(HandlerMethod handlerMethod) { | ||
| 79 | + if (handlerMethod == null) { | ||
| 80 | + return; | ||
| 81 | + } | ||
| 82 | + Method method = handlerMethod.getMethod(); | ||
| 83 | + Class<?> clazz = method.getDeclaringClass(); | ||
| 84 | + try { | ||
| 85 | + // 获取 method 的 lineNumber | ||
| 86 | + List<String> clazzContents = FileUtil.readUtf8Lines( | ||
| 87 | + ResourceUtil.getResource(null, clazz).getPath().replace("/target/classes/", "/src/main/java/") | ||
| 88 | + + clazz.getSimpleName() + ".java"); | ||
| 89 | + Optional<Integer> lineNumber = IntStream.range(0, clazzContents.size()) | ||
| 90 | + .filter(i -> clazzContents.get(i).contains(" " + method.getName() + "(")) // 简单匹配,不考虑方法重名 | ||
| 91 | + .mapToObj(i -> i + 1) // 行号从 1 开始 | ||
| 92 | + .findFirst(); | ||
| 93 | + if (lineNumber.isEmpty()) { | ||
| 94 | + return; | ||
| 95 | + } | ||
| 96 | + // 打印结果 | ||
| 97 | + System.out.printf("\tController 方法路径:%s(%s.java:%d)\n", clazz.getName(), clazz.getSimpleName(), lineNumber.get()); | ||
| 98 | + } catch (Exception ignore) { | ||
| 99 | + // 忽略异常。原因:仅仅打印,非重要逻辑 | ||
| 100 | + } | ||
| 101 | + } | ||
| 102 | + | ||
| 67 | } | 103 | } |