Springboot MDC + logback for log tracing

Posted by robot43298 on Tue, 15 Feb 2022 06:55:24 +0100

1, Introduction to MDC

MDC (Mapped Diagnostic Contexts) maps diagnostic contexts. This feature is a function provided by logback to facilitate logging under multi-threaded conditions.
Some applications use multithreading to process requests from multiple users. During the use of a user, there may be multiple different threads to process. A typical example is a Web application server. When a user accesses a page, the application server may create a new thread to process the request, or reuse the existing thread from the thread pool. During a user's session, multiple threads may have processed the user's request. This makes it difficult to distinguish the logs corresponding to different users. When you need to track the relevant log records of a user in the system, it becomes very troublesome.
MDC is used to solve the above problems. MDC can be regarded as a hash table bound to the current thread, and key value pairs can be added to it. The content contained in MDC can be accessed by code executed in the same thread. The child thread of the current thread will inherit the contents of the MDC in its parent thread. When logging is required, you only need to obtain the required information from MDC. The content of MDC is saved by the program at an appropriate time. For a Web application, this data is usually saved at the beginning of the request being processed.

2, MDC implements log tracking

One system provides services for other systems to call. When other systems call, the request header carries a unique request ID and outputs this requestId to the log. In this way, the two systems will directly form an execution chain and connect them in series with requestId. When an error occurs, you can query the corresponding request log at the caller, You can also query the request log at the server. It is very convenient to locate the problem. There are many places to output the log. You can use MDC to cooperate with the pattern in logback.

3, HandlerInterceptor interceptor

Related introduction:

SpringBoot uses filters, interceptors, AOPs, and their differences and execution order - splitting heaven and building land - blog Garden
HandlerInterceptor_dekulugu's blog - CSDN blog_ handlerinterceptor

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TokenInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(TokenInterceptor.class);

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        //Add MDC log
        String appkey = request.getHeader("appkey");
        MDC.put("LOG_TRACK_ID", appkey);
        return true;

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        //Remove MDC log


4, logback file

<?xml version="1.0" encoding="UTF-8"?>
    <property name="logLevel" value="INFO"></property>
    <property name="logPath" value="./logs"></property>
    <property name="maxHistory" value="31"/>
    <property name="maxFileSize" value="100MB"/>
    <property name="totalSizeCap" value="200GB"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] [%X{logTrackId}] %-5level %logger{35} - %msg %n</pattern>

    <appender name="FILE_INFO" class="ch.qos.logback.classic.sift.SiftingAppender">
        <!--discriminator Discriminator, according to logTrackId this key Corresponding value Identify log events and delegate them to specific appender Write log-->
            <!--Specific log writing appender´╝îevery last logTrackId Create a file-->
            <appender name="FILE-${logTrackId}" class="ch.qos.logback.core.rolling.RollingFileAppender">
                <!--Specify file path directly-->
                <encoder charset="UTF-8">
                    <pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] [%X{logTrackId}] %-5level %logger{35} - %msg%n</pattern>
                <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
                    <!--Defines the format of the file name when the file is scrolled-->
                <filter class="ch.qos.logback.classic.filter.ThresholdFilter">

    <!-- Asynchronous output -->
    <appender name="ASYNC-INFO" class="ch.qos.logback.classic.AsyncAppender">
        <!-- Do not lose logs.default,If 80 of the queue%already expired,Will be discarded TRACT,DEBUG,INFO Level log -->
        <!-- Change the depth of the default queue,This value affects performance.The default value is 256 -->
        <!-- Add additional appender,At most one can be added -->
        <appender-ref ref="FILE_INFO"/>

    <root level="${logLevel}">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE_INFO"/>

Related learning links: New understanding of logback (2): MDC log tracking and log customization effect of logback_ IT-CRUD-CSDN blog

[log tracking] (micro service application and single application) - MDC mechanism in logback - listen to wind and rain - blog Garden


Topics: Java Spring Boot Logback