uuid cost
This commit is contained in:
@@ -1,12 +1,15 @@
|
||||
package com.agileboot.common.web.aspect;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
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.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.util.StopWatch;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
@@ -17,28 +20,37 @@ import java.util.Arrays;
|
||||
@Component
|
||||
public class LogAspect {
|
||||
|
||||
@Value("${agileboot.traceRequestIdKey:W-RequestId}")
|
||||
private String requestIdKey;
|
||||
|
||||
@Pointcut("execution(* *..controller..*.*(..))")
|
||||
public void logPointCut() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Around("logPointCut()")
|
||||
public Object around(ProceedingJoinPoint point) throws Throwable {
|
||||
// 获取HTTP请求对象
|
||||
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
|
||||
|
||||
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
|
||||
HttpServletRequest request = servletRequestAttributes.getRequest();
|
||||
HttpServletResponse resp = servletRequestAttributes.getResponse();
|
||||
// 获取请求URL
|
||||
String url = request.getRequestURI().toString();
|
||||
String uuid = resp.getHeader(requestIdKey);
|
||||
|
||||
// 获取方法参数
|
||||
Object[] args = point.getArgs();
|
||||
String reqParam = Arrays.toString(args); // 直接使用 Arrays.toString()
|
||||
|
||||
// 输出请求日志
|
||||
log.info("request start, path: {}, params: {}", url, reqParam);
|
||||
log.info("request start, path: {}, uuid: {}, params: {}", url, uuid, reqParam);
|
||||
|
||||
StopWatch stopWatch = new StopWatch();
|
||||
stopWatch.start();
|
||||
Object res = point.proceed();
|
||||
stopWatch.stop();
|
||||
long totalTimeMillis = stopWatch.getTotalTimeMillis();
|
||||
log.info("request end, path: {}, uuid: {}, cost: {}ms", url, uuid, totalTimeMillis);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user