Pārlūkot izejas kodu

提交日志格式优化

chenxiaofei 6 mēneši atpakaļ
vecāks
revīzija
d8709dc6b1

+ 41 - 9
sckw-common/sckw-common-log/src/main/java/com/sckw/log/aspect/LogInfoAspect.java

@@ -62,6 +62,7 @@ public class LogInfoAspect {
         Date startTime = new Date();
         String targetName = p.getTarget().getClass().getName();
         String methodName = p.getSignature().getName();
+        String callerMethodName = getCallerMethodName();
         Object[] args = p.getArgs();
         Stream<?> stream = ArrayUtils.isEmpty(args) ? Stream.empty() : Arrays.stream(args);
         List<Object> logArgs = stream
@@ -83,20 +84,21 @@ public class LogInfoAspect {
             Boolean slowRequest = (time > 1500L);
 
             if (exception != null && !exception.isEmpty()) {
-                log.error("\nAPI调用异常 - {}.{} \n参数:{} \n结果:{} \n异常:{} \n耗时:{}ms \n慢请求:{}",
-                        targetName, methodName, param, JSON.toJSONString(result), exception, time, slowRequest);
+                log.error("\nAPI调用异常 - {}.{} \n调用方 - {} \n参数:{} \n结果:{} \n异常:{} \n耗时:{}ms \n慢请求:{}",
+                        targetName, methodName, callerMethodName, param, JSON.toJSONString(result), exception, time, slowRequest);
             } else if (slowRequest) {
-                log.warn("\nAPI慢请求 - {}.{} \n参数:{} \n结果:{} \n耗时:{}ms \n慢请求:{}",
-                        targetName, methodName, param, JSON.toJSONString(result), time, slowRequest);
+                log.warn("\nAPI慢请求 - {}.{} \n调用方 - {} \n参数:{} \n结果:{} \n耗时:{}ms \n慢请求:{}",
+                        targetName, methodName, callerMethodName, param, JSON.toJSONString(result), time, slowRequest);
             } else {
-                log.info("\nAPI调用成功 - {}.{} \n参数:{} \n结果:{} \n耗时:{}ms",
-                        targetName, methodName, param, JSON.toJSONString(result), time);
+                log.info("\nAPI调用成功 - {}.{} \n调用方 - {} \n参数:{} \n结果:{} \n耗时:{}ms",
+                        targetName, methodName, callerMethodName, param, JSON.toJSONString(result), time);
             }
 
             // 原始详细日志保留为debug级别
-            log.debug("\n接口调用 - {}.{}\nparam={}\nresult={}\nexception={}\n[{}->{}],slowRequest{}=[{}]",
+            log.debug("\n接口调用 - {}.{} \n调用方 - {}\nparam={}\nresult={}\nexception={}\n[{}->{}],slowRequest{}=[{}]",
                     targetName,
                     methodName,
+                    callerMethodName,
                     param,
                     JSON.toJSONString(result),
                     exception,
@@ -108,15 +110,44 @@ public class LogInfoAspect {
         return result;
     }
 
+    /**
+     * 获取调用方的方法名称
+     * @return 调用方方法名称
+     */
+    private String getCallerMethodName() {
+        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
+        // 查找调用链中属于业务代码的部分
+        for (int i = 0; i < stackTrace.length; i++) {
+            StackTraceElement element = stackTrace[i];
+            // 找到LogInfoAspect类的调用点
+            if (element.getClassName().equals(LogInfoAspect.class.getName()) && 
+                ("cutController".equals(element.getMethodName()) || "around".equals(element.getMethodName()))) {
+                // 继续向上查找真正的调用者
+                for (int j = i + 1; j < stackTrace.length; j++) {
+                    StackTraceElement caller = stackTrace[j];
+                    // 排除Java内置类和框架类
+                    if (!caller.getClassName().startsWith("java.") && 
+                        !caller.getClassName().startsWith("org.springframework.") &&
+                        !caller.getClassName().startsWith("org.aspectj.") &&
+                        !caller.getClassName().equals(LogInfoAspect.class.getName())) {
+                        return caller.getClassName() + "." + caller.getMethodName();
+                    }
+                }
+            }
+        }
+        return "Unknown";
+    }
+
     @Pointcut("execution(* com.sckw.*.service..*.*(..))")
     public void cutService() {
     }
 
     @AfterThrowing(pointcut = "cutService()", throwing = "e")
-    public void doAfterThrowing(JoinPoint point, Throwable e) throws Throwable {
+public void doAfterThrowing(JoinPoint point, Throwable e) throws Throwable {
         Date startTime = new Date();
         String targetName = point.getTarget().getClass().getName();
         String methodName = point.getSignature().getName();
+        String callerMethodName = getCallerMethodName();
         //获取用户请求方法的参数并序列化为JSON格式字符串
         StringBuilder params = new StringBuilder();
         if (point.getArgs() != null && point.getArgs().length > 0) {
@@ -130,8 +161,9 @@ public class LogInfoAspect {
 
         Date endTime = new Date();
         long time = endTime.getTime() - startTime.getTime();
-        log.error("\nSERVICE异常 - {}.{} \n参数:{} \n异常:{} \n耗时:{}ms \n时间:[{}->{}]",
+        log.error("\nSERVICE异常 - {}.{} \n调用方 - {} \n参数:{} \n异常:{} \n耗时:{}ms \n时间:[{}->{}]",
                 targetName, methodName,
+                callerMethodName,
                 params,
                 e.getMessage(),
                 time,