欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

AOP+MDC实现traceId日志追踪

程序员文章站 2022-03-05 08:07:41
...
/**
 * 统一日志追踪处理器
 *
 * @author dengy
 * @since jdk1.8
 */

@Slf4j
@Component
@Aspect
public class TraceIdHandler {

    private static final String TRACE_ID = "traceId";

    @Before(value = "execution(* com.xxx.exbiz.trans..*.*(..))")
    public void aroundHandle() {
        if (StringUtils.isBlank(MDC.get(TRACE_ID))) {
            String traceId = DateUtil.getSysTime() + "-" + ((int) ((Math.random() * 9 + 1) * 100000));
            MDC.put(TRACE_ID, traceId);
        }
    }
}
关键是--> [%X{traceId}]

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<encoder class="com.llpay.framework.autoconfigure.logging.logback.SecurePatternLayoutEncoder">
	        <pattern>%d %-5p [%X{traceId}] %t [%c] - %m%n</pattern>
	        <charset>UTF-8</charset>
		</encoder>
	</appender>

 

转载于:https://my.oschina.net/dyyweb/blog/2980044