|
@@ -1,6 +1,6 @@
|
|
|
package com.nokia.domainb.config;
|
|
|
|
|
|
-import com.alibaba.fastjson2.JSON;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.aspectj.lang.JoinPoint;
|
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
@@ -8,7 +8,9 @@ import org.aspectj.lang.annotation.Around;
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
import org.aspectj.lang.annotation.Before;
|
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
|
+import org.slf4j.MDC;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.util.StopWatch;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
@@ -17,22 +19,50 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
import javax.servlet.ServletRequest;
|
|
|
import javax.servlet.ServletResponse;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
- * 日志切面
|
|
|
+ * 日志配置
|
|
|
+ *
|
|
|
*/
|
|
|
@Aspect
|
|
|
@Component
|
|
|
@Slf4j
|
|
|
public class LogAspectConfig
|
|
|
{
|
|
|
+ @Pointcut("execution(* com.nokia..ControllerExceptionHandler.*ValidatorHandler(..))")
|
|
|
+ public void validatorExceptionHandlerPointcut()
|
|
|
+ {
|
|
|
+ // Pointcut
|
|
|
+ }
|
|
|
+
|
|
|
@Pointcut("execution(public * com.nokia..*Controller.*(..))")
|
|
|
public void controllerPointcut()
|
|
|
{
|
|
|
// Pointcut
|
|
|
}
|
|
|
|
|
|
- @Before("controllerPointcut()")
|
|
|
+ @Pointcut("execution(* com.nokia..ControllerExceptionHandler.*(..))")
|
|
|
+ public void controllerExceptionHandlerPointcut()
|
|
|
+ {
|
|
|
+ // Pointcut
|
|
|
+ }
|
|
|
+
|
|
|
+ @Pointcut("controllerPointcut() || validatorExceptionHandlerPointcut()")
|
|
|
+ public void beforePointCut()
|
|
|
+ {
|
|
|
+ // Pointcut
|
|
|
+ }
|
|
|
+
|
|
|
+ @Pointcut("controllerPointcut() || controllerExceptionHandlerPointcut()")
|
|
|
+ public void afterPointCut()
|
|
|
+ {
|
|
|
+ // Pointcut
|
|
|
+ }
|
|
|
+
|
|
|
+ @Before("beforePointCut()")
|
|
|
public void doBefore(JoinPoint joinPoint)
|
|
|
{
|
|
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
@@ -41,32 +71,36 @@ public class LogAspectConfig
|
|
|
return;
|
|
|
}
|
|
|
HttpServletRequest request = attributes.getRequest();
|
|
|
+ // 打印请求信息
|
|
|
log.info("请求地址: {} {}", request.getRequestURL().toString(), request.getMethod());
|
|
|
+ // 打印请求参数
|
|
|
Object[] args = joinPoint.getArgs();
|
|
|
- Object[] arguments = new Object[args.length];
|
|
|
- for (int i = 0; i < args.length; i++)
|
|
|
- {
|
|
|
- if (args[i] instanceof ServletRequest
|
|
|
- || args[i] instanceof ServletResponse
|
|
|
- || args[i] instanceof MultipartFile
|
|
|
- || args[i] instanceof Exception
|
|
|
- )
|
|
|
- {
|
|
|
+ List<Object> list = new ArrayList<>();
|
|
|
+ for (Object arg : args) {
|
|
|
+ if (arg instanceof ServletRequest
|
|
|
+ || arg instanceof ServletResponse
|
|
|
+ || arg instanceof MultipartFile
|
|
|
+ || arg instanceof Exception
|
|
|
+ ) {
|
|
|
continue;
|
|
|
}
|
|
|
- arguments[i] = args[i];
|
|
|
+ list.add(arg);
|
|
|
+ }
|
|
|
+ if (CollectionUtils.isEmpty(list)) {
|
|
|
+ return;
|
|
|
}
|
|
|
- log.info("入参: {}", JSON.toJSONString(arguments));
|
|
|
+ log.info("请求参数: {}", JSON.toJSONString(list));
|
|
|
}
|
|
|
|
|
|
- @Around("controllerPointcut()")
|
|
|
+ @Around("afterPointCut()")
|
|
|
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
|
|
|
{
|
|
|
StopWatch stopWatch = new StopWatch();
|
|
|
stopWatch.start();
|
|
|
+ MDC.put("traceId", UUID.randomUUID().toString().replace("-", ""));
|
|
|
Object result = proceedingJoinPoint.proceed();
|
|
|
- log.info("结果: {}", JSON.toJSONString(result));
|
|
|
stopWatch.stop();
|
|
|
+ log.info("返回: {}", JSON.toJSONString(result));
|
|
|
log.info("耗时:{} ms", stopWatch.getTotalTimeMillis());
|
|
|
return result;
|
|
|
}
|