https://www.jianshu.com/p/1dea7479eb07
https://logback.qos.ch/xref/chapters/mdc/SimpleMDC.html
调整log pattern
1 2
|
# 引用mdc中的mingId变量 %X{变量名称} %X{mingId}-%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50}- %msg%n
|
代码示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
package com.ming;
import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.slf4j.MDC; import org.springframework.boot.test.context.SpringBootTest;
import java.util.UUID;
@SpringBootTest(classes = Start.class) @Slf4j public class TestSlf4jMDC { @Test public void test() { MDC.put("mingId", UUID.randomUUID().toString()); log.info("testMingId"); MDC.clear(); log.info("testMingIdClean"); } }
|
输出日志
1 2 3 4
|
#日期前的就是设置的mingId d4b03ec6-bea4-452d-8f47-45c9fd03b1ab-2021-11-25 14:21:15.539 [main] INFO com.ming.TestSlf4jMDC- testMingId #当没有mingId 的时候 不显示 -2021-11-25 14:21:15.539 [main] INFO com.ming.TestSlf4jMDC- testMingIdClean
|