Ver código fonte

fix: 链路追踪id日志打印

regan 2 anos atrás
pai
commit
c15c7a307d

+ 9 - 4
iot-common/iot-common-web/src/main/java/cc/iotkit/common/web/interceptor/PlusWebInvokeTimeInterceptor.java

@@ -1,5 +1,6 @@
 package cc.iotkit.common.web.interceptor;
 
+import cc.iotkit.common.api.Request;
 import cc.iotkit.common.utils.JsonUtils;
 import cc.iotkit.common.utils.SpringUtils;
 import cc.iotkit.common.utils.StringUtils;
@@ -9,6 +10,7 @@ import cn.hutool.core.map.MapUtil;
 import com.alibaba.ttl.TransmittableThreadLocal;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.time.StopWatch;
+import org.slf4j.MDC;
 import org.springframework.http.MediaType;
 import org.springframework.web.servlet.HandlerInterceptor;
 import org.springframework.web.servlet.ModelAndView;
@@ -43,15 +45,17 @@ public class PlusWebInvokeTimeInterceptor implements HandlerInterceptor {
                 if (request instanceof RepeatedlyRequestWrapper) {
                     BufferedReader reader = request.getReader();
                     jsonParam = IoUtil.read(reader);
+                    Request req = JsonUtils.parseObject(jsonParam,Request.class);
+                    MDC.put("requestId",req.getRequestId());
                 }
-                log.debug("[PLUS]开始请求 => URL[{}],参数类型[json],参数:[{}]", url, jsonParam);
+                log.debug("开始请求 => URL[{}],参数类型[json],参数:[{}]", url, jsonParam);
             } else {
                 Map<String, String[]> parameterMap = request.getParameterMap();
                 if (MapUtil.isNotEmpty(parameterMap)) {
                     String parameters = JsonUtils.toJsonString(parameterMap);
-                    log.debug("[PLUS]开始请求 => URL[{}],参数类型[param],参数:[{}]", url, parameters);
+                    log.debug("开始请求 => URL[{}],参数类型[param],参数:[{}]", url, parameters);
                 } else {
-                    log.debug("[PLUS]开始请求 => URL[{}],无参数", url);
+                    log.debug("开始请求 => URL[{}],无参数", url);
                 }
             }
 
@@ -72,8 +76,9 @@ public class PlusWebInvokeTimeInterceptor implements HandlerInterceptor {
         if (!prodProfile.equals(SpringUtils.getActiveProfile())) {
             StopWatch stopWatch = invokeTimeTL.get();
             stopWatch.stop();
-            log.debug("[PLUS]结束请求 => URL[{}],耗时:[{}]毫秒", request.getMethod() + " " + request.getRequestURI(), stopWatch.getTime());
+            log.debug("结束请求 => URL[{}],耗时:[{}]毫秒", request.getMethod() + " " + request.getRequestURI(), stopWatch.getTime());
             invokeTimeTL.remove();
+            MDC.clear();
         }
     }
 

+ 1 - 1
iot-starter/src/main/resources/logback-spring.xml

@@ -7,7 +7,7 @@
 
     <!-- You can override this to have a custom pattern -->
     <property name="CONSOLE_LOG_PATTERN"
-              value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
+              value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} [%X{requestId}] %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
 
     <!-- Appender to log to console -->
     <appender name="console" class="ch.qos.logback.core.ConsoleAppender">

+ 1 - 2
iot-test-tool/pom.xml

@@ -13,9 +13,8 @@
         提供用于测试的相关工具的模块
     </description>
 
-
     <modules>
-        <module>iot-test-mqtt</module>
+       <!-- <module>iot-test-mqtt</module>-->
         <module>iot-virtual-device</module>
     </modules>