描述
通常我们出现生产业务问题时,需要找到关键日志信息,再把该日志对应的请求的所有日志捞出,找到问题原因;
请求的关联id就是TraceId,如下都是基于MDC对TraceId的实现。
实现
-
定义拦截器
package com.satan.common.utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import java.util.UUID;
@Slf4j
public class TraceUtils {
private static final String TRACE_ID = "traceId";
public static void createTraceId() {
String traceId = MDC.get(TRACE_ID);
if (StringUtils.isBlank(traceId)) {
traceId = UUID.randomUUID().toString().replaceAll("-", "").toLowerCase();
log.debug("create traceId :{}", traceId);
MDC.put(TRACE_ID, traceId);
}
}
public static void destroyTraceId() {
MDC.remove(TRACE_ID);
}
}
package com.satan.common.interceptor;
import com.satan.common.utils.TraceUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TraceIdInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
TraceUtils.createTraceId();
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
TraceUtils.destroyTraceId();
}
}
package com.satan.product.config;
import com.satan.common.interceptor.TraceIdInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new TraceIdInterceptor()).addPathPatterns("/**");
}
}
-
logback配置traceId
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d [%thread][%X{traceId}] %-5p [%c] [%F:%L] - %msg%n</pattern>
</encoder>
</appender>
<logger name="chapters.configuration" level="INFO"/>
<root level="INFO">
<appender-ref ref="STDOUT"/>
</root>
</configuration>
-
效果
2021-07-25 22:56:10,000 [com.alibaba.nacos.client.naming.updater][] INFO [com.alibaba.nacos.client.naming] [HostReactor.java:267] - current ips:(1) service: DEFAULT_GROUP@@life-customer@@DEFAULT -> [{"instanceId":"******#8071#DEFAULT#DEFAULT_GROUP@@life-customer","ip":"******","port":8071,"weight":1.0,"healthy":true,"enabled":true,"ephemeral":true,"clusterName":"DEFAULT","serviceName":"DEFAULT_GROUP@@life-customer","metadata":{"preserved.register.source":"SPRING_CLOUD"},"ipDeleteTimeout":30000,"instanceHeartBeatInterval":5000,"instanceHeartBeatTimeOut":15000}]
2021-07-25 22:56:33,226 [http-nio-8071-exec-1][] INFO [org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/]] [DirectJDKLog.java:173] - Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-07-25 22:56:33,226 [http-nio-8071-exec-1][] INFO [org.springframework.web.servlet.DispatcherServlet] [FrameworkServlet.java:525] - Initializing Servlet 'dispatcherServlet'
2021-07-25 22:56:33,230 [http-nio-8071-exec-1][] INFO [org.springframework.web.servlet.DispatcherServlet] [FrameworkServlet.java:547] - Completed initialization in 4 ms
2021-07-25 22:56:33,244 [http-nio-8071-exec-1][] WARN [org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver] [AbstractHandlerExceptionResolver.java:207] - Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
2021-07-25 22:56:50,386 [http-nio-8071-exec-3][af8bf1254fa746598fc87074ae5c1707] INFO [com.satan.customer.controller.TestController] [TestController.java:16] - business step 0 complete
2021-07-25 22:56:50,386 [http-nio-8071-exec-3][af8bf1254fa746598fc87074ae5c1707] INFO [com.satan.customer.controller.TestController] [TestController.java:16] - business step 1 complete
2021-07-25 22:56:50,387 [http-nio-8071-exec-3][af8bf1254fa746598fc87074ae5c1707] INFO [com.satan.customer.controller.TestController] [TestController.java:16] - business step 2 complete
2021-07-25 22:56:50,387 [http-nio-8071-exec-3][af8bf1254fa746598fc87074ae5c1707] INFO [com.satan.customer.controller.TestController] [TestController.java:16] - business step 3 complete
2021-07-25 22:56:50,387 [http-nio-8071-exec-3][af8bf1254fa746598fc87074ae5c1707] INFO [com.satan.customer.controller.TestController] [TestController.java:16] - business step 4 complete
2021-07-25 22:56:50,387 [http-nio-8071-exec-3][af8bf1254fa746598fc87074ae5c1707] INFO [com.satan.customer.controller.TestController] [TestController.java:16] - business step 5 complete
2021-07-25 22:56:50,388 [http-nio-8071-exec-3][af8bf1254fa746598fc87074ae5c1707] INFO [com.satan.customer.controller.TestController] [TestController.java:16] - business step 6 complete
2021-07-25 22:56:50,388 [http-nio-8071-exec-3][af8bf1254fa746598fc87074ae5c1707] INFO [com.satan.customer.controller.TestController] [TestController.java:16] - business step 7 complete
2021-07-25 22:56:50,388 [http-nio-8071-exec-3][af8bf1254fa746598fc87074ae5c1707] INFO [com.satan.customer.controller.TestController] [TestController.java:16] - business step 8 complete
2021-07-25 22:56:50,388 [http-nio-8071-exec-3][af8bf1254fa746598fc87074ae5c1707] INFO [com.satan.customer.controller.TestController] [TestController.java:16] - business step 9 complete