This commit is contained in:
jiangh277
2025-08-04 16:51:13 +08:00
parent f8fb9b561c
commit eba0eb085e
41 changed files with 451 additions and 73 deletions

View File

@@ -0,0 +1,48 @@
package com.timeline.common.aop;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
@Slf4j
@Aspect
@Component
public class RequestLogAspect {
@Pointcut("execution(* com..controller.*.*(..))")
public void log() {
}
@Around("log()")
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
try {
// 获取请求信息
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
log.info("【请求开始】");
log.info("接受请求, URL : {}", request.getRequestURL());
log.info("HTTP Method : {}", request.getMethod());
log.info("IP Address : {}", request.getRemoteAddr());
log.info("Class Method: {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());
// 执行原方法
Object result = joinPoint.proceed();
// 输出返回值
log.info("响应结果 : {}", result);
log.info("结束请求, 请求耗时 : {} ms", System.currentTimeMillis() - startTime);
return result;
} catch (Exception e) {
log.error("请求发生异常", e);
throw e;
}
}
}