describe

Usually, when a production service problem occurs, you need to find the key log information and then fish out all logs of the request corresponding to the log to find the cause of the problem. The associated ID of the request is TraceId. The following are based on the MDC implementation of TraceId.Copy the code

implementation

  1. Defining interceptors

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(a) {
        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(a) { MDC.remove(TRACE_ID); }}Copy the code
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(); }}Copy the code
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("/ * *"); }}Copy the code
  1. Logback configuration traceId


      
<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>
Copy the code
  1. The effect

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
Copy the code