From 33fc74936305441fa5d2ab8ccf7e7f2e03e3acf9 Mon Sep 17 00:00:00 2001 From: wol <1293433164@qq.com> Date: Sun, 21 Sep 2025 19:46:30 +0800 Subject: [PATCH] uuid cost --- .../common/web/aspect/LogAspect.java | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/agileboot-common/wol-common-web/src/main/java/com/agileboot/common/web/aspect/LogAspect.java b/agileboot-common/wol-common-web/src/main/java/com/agileboot/common/web/aspect/LogAspect.java index 21289e1..2de6e28 100644 --- a/agileboot-common/wol-common-web/src/main/java/com/agileboot/common/web/aspect/LogAspect.java +++ b/agileboot-common/wol-common-web/src/main/java/com/agileboot/common/web/aspect/LogAspect.java @@ -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; } }