fix: 链路追踪id日志打印

V0.5.x
regan 2023-06-17 19:06:46 +08:00
parent d802d68aaa
commit c15c7a307d
3 changed files with 11 additions and 7 deletions

View File

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

View File

@ -7,7 +7,7 @@
<!-- You can override this to have a custom pattern --> <!-- You can override this to have a custom pattern -->
<property name="CONSOLE_LOG_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 to log to console -->
<appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <appender name="console" class="ch.qos.logback.core.ConsoleAppender">

View File

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