Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pcm committed Oct 30, 2024
1 parent c537f9d commit e6b4f04
Showing 1 changed file with 36 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -22,7 +26,8 @@
@Slf4j
@Order(-1)
public class LogAspect {

// 使用ThreadLocal存储请求唯一ID
private static final ThreadLocal<String> requestId = ThreadLocal.withInitial(() -> null);

/**
* @Title: logExecution
Expand All @@ -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());

}

/**
Expand All @@ -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));
}


Expand All @@ -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();
}

}

0 comments on commit e6b4f04

Please sign in to comment.