From e6b4f04fc52675fa4ec4830327590fbc969cad99 Mon Sep 17 00:00:00 2001 From: pcm Date: Wed, 30 Oct 2024 16:43:12 +0800 Subject: [PATCH] update --- .../com/pancm/commons/aspect/LogAspect.java | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/springboot-summarizing/src/main/java/com/pancm/commons/aspect/LogAspect.java b/springboot-summarizing/src/main/java/com/pancm/commons/aspect/LogAspect.java index 90a9623..41abe51 100644 --- a/springboot-summarizing/src/main/java/com/pancm/commons/aspect/LogAspect.java +++ b/springboot-summarizing/src/main/java/com/pancm/commons/aspect/LogAspect.java @@ -1,14 +1,18 @@ package com.pancm.commons.aspect; +import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.*; +import org.slf4j.MDC; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; +import java.util.UUID; + /** * @Author pancm @@ -22,7 +26,8 @@ @Slf4j @Order(-1) public class LogAspect { - + // 使用ThreadLocal存储请求唯一ID + private static final ThreadLocal requestId = ThreadLocal.withInitial(() -> null); /** * @Title: logExecution @@ -39,7 +44,16 @@ public void ponitcut() { */ @Before("ponitcut()") public void before(JoinPoint joinPoint) { - log.info("{}|请求ip:{},内容:{}", getHeadId(), getIp(), joinPoint.getArgs()); + //生成唯一ID并存储到ThreadLocal + String uniqueId = UUID.randomUUID().toString().replaceAll("-", ""); + requestId.set(uniqueId); + // 将唯一ID放入MDC,以便在日志中全局可见 + MDC.put("requestId", uniqueId); + Class className = joinPoint.getTarget().getClass(); + String methodName = joinPoint.getSignature().getName(); + String path = className.getName().concat("#").concat(methodName); + log.info("{}|正常请求,请求IP:{},请求人:{},请求路径:{},\n请求内容:{}", uniqueId, getClientIp(), "", path, joinPoint.getArgs()); + } /** @@ -52,7 +66,12 @@ public void before(JoinPoint joinPoint) { */ @AfterReturning(pointcut = "ponitcut()", returning = "returnValue") public void doafterReturning(JoinPoint joinPoint, Object returnValue) { - log.info("{}|返回ip:{},内容:{}", getHeadId(), getIp(), returnValue); + // 获取存储的唯一ID + String uniqueId = requestId.get(); + Class className = joinPoint.getTarget().getClass(); + String methodName = joinPoint.getSignature().getName(); + String path = className.getName().concat("#").concat(methodName); + log.info("{}|正常返回,返回路径:{},\n请求内容:{},\n返回内容:{}", uniqueId,path, joinPoint.getArgs(), JSONObject.toJSONString(returnValue)); } @@ -64,18 +83,27 @@ public void doafterReturning(JoinPoint joinPoint, Object returnValue) { */ @AfterThrowing(pointcut = "ponitcut()", throwing = "e") public void doAfterThrowing(JoinPoint joinPoint, Throwable e) { + String uniqueId = requestId.get(); Class className = joinPoint.getTarget().getClass(); String methodName = joinPoint.getSignature().getName(); - log.warn("{}|异常返回ip:{},class:{},method:{},异常内容:{}", getHeadId(), getIp(), className.getName(), methodName, e.getMessage()); + String path = className.getName().concat("#").concat(methodName); + log.error("{}|异常返回,请求IP:{},返回路径:{},\n返回内容:{},\n异常信息:{},",uniqueId, getClientIp(), path, joinPoint.getArgs(), e.getMessage()); } - public String getIp() { - return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRemoteAddr(); + + + /** + * 清除ThreadLocal中的请求ID + */ + @After("ponitcut()") + public void cleanUp() { + requestId.remove(); + MDC.remove("requestId"); } - private String getHeadId() { - return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getHeader("reqId"); + public String getClientIp() { + return ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRemoteAddr(); } }