<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight(%-5level) [%X{_traceId}] --- [%15.15(%thread)] %cyan(%-40.40(%logger{40})) : %msg%n</pattern>
package top.hundreds.homeworkhelper.common.config.tomcat; import cn.hutool.core.util.IdUtil; import org.slf4j.MDC; import org.springframework.core.annotation.Order; import org.springframework.util.StringUtils; import javax.servlet.*; import javax.servlet.annotation.WebFilter; import java.io.IOException; /** * slf4j 线程日志设置 * @Author lyr * @create 2020/10/12 19:46 */ @Order(1) @WebFilter(urlPatterns = "/*") public class LogTraceFilter implements Filter { private static final String TRACE_ID = "_traceId"; @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { String traceId = servletRequest.getParameter(TRACE_ID); if(StringUtils.isEmpty(traceId)) { traceId = IdUtil.fastSimpleUUID(); } MDC.put(TRACE_ID,traceId); filterChain.doFilter(servletRequest, servletResponse); } }