Abstract: This example is mainly based on SLF4J MDC implementation of tranceId context saving, of course, can also define their own ThreadLocal to save, the way, for cross-service invocation can also put tranceId in the request header, the receiver reuse the tranceId

Based on theFilterThe implementation of the

Intercept all requests

@WebFilter(filterName = "traceIdFilter", urlPatterns = "/*") @Component public class TraceFilter extends OncePerRequestFilter { @Override protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException { MDC.put("tranceId", UUID.randomUUID().toString()); filterChain.doFilter(httpServletRequest,httpServletResponse); }}Copy the code

logback.xmlconfiguration

<! -- Logback configuration. See http://logback.qos.ch/manual/index.html --> <configuration> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%date{HH:mm:ss.SSS} | %X{tranceId} | [%thread] %-5level %logger{36} - %msg%n </pattern> </encoder> </appender> <logger name="org.springframework.web" level="INFO"/> <logger name="com.huzhihui" level="INFO"/> <! <root level="INFO"> <appender-ref ref="STDOUT"/> </root> </configuration>Copy the code

Defining the controller

@Slf4j @RestController public class CommonController { @RequestMapping(value = "i18n") public Object i18n(){ log.info("ttts"); log.info("bafgsdaf"); return message; }}Copy the code