no message
This commit is contained in:
54
src/main/java/com/sczx/pay/aspect/FacadeAspect.java
Normal file
54
src/main/java/com/sczx/pay/aspect/FacadeAspect.java
Normal file
@ -0,0 +1,54 @@
|
||||
package com.sczx.pay.aspect;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
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.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @Author Huang Kai
|
||||
* @Date 2023/1/18 17:01
|
||||
* @Version 1.0
|
||||
*/
|
||||
@Aspect
|
||||
@Component
|
||||
@Slf4j
|
||||
public class FacadeAspect {
|
||||
|
||||
|
||||
|
||||
/** 以 controller 包下定义的所有请求为切入点 */
|
||||
@Pointcut("execution(public * com..facade.*.*(..))")
|
||||
public void facadeLog() {}
|
||||
|
||||
/**
|
||||
* 在切点之前织入
|
||||
* @param joinPoint
|
||||
*/
|
||||
@Before("facadeLog()")
|
||||
public void doBefore(JoinPoint joinPoint) {
|
||||
// 打印请求相关参数
|
||||
log.info("******** remote Start *********");
|
||||
// 打印 Http method
|
||||
//log.info("HTTP Method : {}", request.getMethod());
|
||||
// 打印调用 controller 的全路径以及执行方法
|
||||
log.info("Class Method:{}.{}, facadeAspect_Request:{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName(),JSON.toJSONString(joinPoint.getArgs()));
|
||||
}
|
||||
|
||||
@Around("facadeLog()")
|
||||
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
|
||||
long startTime = System.currentTimeMillis();
|
||||
Object result = proceedingJoinPoint.proceed();
|
||||
// 打印出参
|
||||
log.info("facadeAspect_Response:{}", JSON.toJSONString(result));
|
||||
// 执行耗时
|
||||
log.info("Time-Consuming : {} ms", System.currentTimeMillis() - startTime);
|
||||
log.info("**************remote End ****************");
|
||||
return result;
|
||||
}
|
||||
}
|
||||
127
src/main/java/com/sczx/pay/aspect/WebLogAspect.java
Normal file
127
src/main/java/com/sczx/pay/aspect/WebLogAspect.java
Normal file
@ -0,0 +1,127 @@
|
||||
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) {
|
||||
// 检查是否是支付宝回调控制器
|
||||
String className = joinPoint.getTarget().getClass().getSimpleName();
|
||||
if ("AliPayNotifyController".equals(className)) {
|
||||
// 对于支付宝回调,简化日志处理
|
||||
log.info("========================= Start =========================");
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
if (attributes != null) {
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
log.info("URL:{}", request.getRequestURL().toString());
|
||||
}
|
||||
String requestId = UUID.randomUUID().toString();
|
||||
// 使用 requestId 的一部分作为日志标识
|
||||
MDC.put("requestId", requestId.substring(Math.max(0, requestId.length() - 12)));
|
||||
return;
|
||||
}
|
||||
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