增加controller日志
This commit is contained in:
112
src/main/java/com/sczx/pay/aspect/WebLogAspect.java
Normal file
112
src/main/java/com/sczx/pay/aspect/WebLogAspect.java
Normal file
@ -0,0 +1,112 @@
|
||||
package com.sczx.pay.aspect;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.annotation.*;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @Author Huang Kai
|
||||
* @Date 2023/1/18 17:01
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@Slf4j
|
||||
public class WebLogAspect {
|
||||
|
||||
|
||||
|
||||
/** 以 controller 包下定义的所有请求为切入点 */
|
||||
@Pointcut("execution(public * com.sczx.*.controller.*.*(..))")
|
||||
public void webLog() {}
|
||||
|
||||
/**
|
||||
* 在切点之前织入
|
||||
* @param joinPoint
|
||||
*/
|
||||
@Before("webLog()")
|
||||
public void doBefore(JoinPoint joinPoint) {
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = null;
|
||||
String requestId = null;
|
||||
|
||||
if (attributes != null) {
|
||||
request = attributes.getRequest();
|
||||
// 从请求头中获取 requestId
|
||||
requestId = request.getHeader("requestId");
|
||||
}
|
||||
|
||||
// 如果请求头中没有 requestId,则生成新的
|
||||
if (StringUtils.isBlank(requestId)) {
|
||||
requestId = UUID.randomUUID().toString();
|
||||
}
|
||||
|
||||
// 使用 requestId 的一部分作为日志标识
|
||||
MDC.put("requestId", requestId.substring(Math.max(0, requestId.length() - 12)));
|
||||
// 打印请求相关参数
|
||||
log.info("========================= Start =========================");
|
||||
// 打印请求入参
|
||||
try{
|
||||
Object[] args = joinPoint.getArgs();
|
||||
List<Object> filteredArgs = new ArrayList<>();
|
||||
for (Object arg : args) {
|
||||
//如果是文件类,则只打印文件名称和大小
|
||||
if (arg instanceof MultipartFile) {
|
||||
MultipartFile file = (MultipartFile) arg;
|
||||
JSONObject fileJson = new JSONObject();
|
||||
fileJson.put("filename", file.getOriginalFilename());
|
||||
fileJson.put("size", file.getSize());
|
||||
filteredArgs.add(fileJson);
|
||||
} else {
|
||||
filteredArgs.add(arg);
|
||||
}
|
||||
}
|
||||
Map<String, Object> paramMap = new HashMap<>();
|
||||
paramMap.put("args", filteredArgs);
|
||||
|
||||
if (request != null) {
|
||||
String token = request.getHeader("Authorization");
|
||||
if(StringUtils.isNotBlank(token)){
|
||||
paramMap.put("Authorization", token);
|
||||
}
|
||||
log.info("URL:{} , {}", request.getRequestURL().toString(), JSON.toJSONString(paramMap));
|
||||
}
|
||||
} catch (Exception e){
|
||||
log.error("打印WebLogAspect_Request失败,args无法被序列化", e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理完请求后执行
|
||||
* @param joinPoint
|
||||
* @param result
|
||||
*/
|
||||
@AfterReturning(pointcut = "webLog()", returning = "result")
|
||||
public void doAfterReturning(JoinPoint joinPoint, Object result) {
|
||||
// 打印出参
|
||||
try {
|
||||
log.info("WebLogAspect_Response:{}", JSON.toJSON(result));
|
||||
}catch (Exception e){
|
||||
log.error("打印WebLogAspect_Response失败,result无法被序列化", e);
|
||||
}
|
||||
log.info("========================= End =========================");
|
||||
MDC.clear();
|
||||
}
|
||||
|
||||
@AfterThrowing(throwing="ex",pointcut="webLog()")
|
||||
public void doRecoveryActions(Throwable ex){
|
||||
MDC.clear();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user