ofpay / logback-mdc-ttl Goto Github PK
View Code? Open in Web Editor NEWlogback扩展,集成transmittable-thread-local支持跨线程池的mdc跟踪
License: Apache License 2.0
logback扩展,集成transmittable-thread-local支持跨线程池的mdc跟踪
License: Apache License 2.0
logback从1.1.5版本之后,新的commit将MDC内部维护的InheritThreadLocal修改成了ThreadLocal,MDC不支持线程继承了。
Child threads no longer inherit MDC values. In previous versions of logback as well as log4j 1.x, MDC values were inherited by child threads. Several users have argued convincingly that MDC inheritance by child threads was unhelpful and even dangerous. This change fixes LOGBACK-422 and LOGBACK-624
https://logback.qos.ch/news.html
如题
java17 支持吗?
http://mvnrepository.com/artifact/com.ofpay 没有logback-mdc-ttl 。
建议发到Maven**库中(不要是SNAPSHOT版本),方便大家直接使用。
transmittable-thread-local 2.6.0
logback-mdc-ttl 1.0.2
logback-classic 1.2.3
-javaagent:/Users/transmittable-thread-local-2.6.0.jar -Xbootclasspath/a:/Users/transmittable-thread-local-2.6.0.jar
springboot 2.0 运行一段时间之后. tomcat 线程池里面的线程 都已经 初始化完毕.
这个时候
两个HTTP请求
A请求, 第0秒开始执行, 第20s执行完毕.
B请求, 第10秒开始执行, 第12秒执行完毕.
第5秒, A请求打的 mdc里面的 traceId 是 111
第12秒, B请求打的 mdc里面的 traceId 是 222
第18秒,A请求打的mdc里面的 traceId 是 222 (问题出现在这里.)
辛苦看一下
使用了
transmittable-thread-local 2.8.1 & logback-mdc-ttl 1.0.2
代码示例:
TtlMDCAdapter.getInstance();
int nThreads = 10;
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
for (int i = 0; i < nThreads; i++) {
MDC.put("key", "value:" + i);
executorService.execute(() -> {
int random = new Random().nextInt(1000);
try {
TimeUnit.MILLISECONDS.sleep(random);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(MDC.get("key"));
});
}
TimeUnit.SECONDS.sleep(3);
期望输出:
value:0
value:2
value:1
xxx
value 0-9无重复数字
实际输出:
value:9
value:9
value:9
value:9
value:9
value:9
value:9
value:9
value:9
实际测下来 好像指定来javaagent 就可以了,不加不知道有什么影响?
您好,TtlMDCAdapter直接就放在一个org.slf4j包里会不会有问题呢?MDC的静态设置MDCAdapter没有别的方法吗?或许可以使用反射解决?
我在验证slf4j+logback的MDC时,是支持在多线程中输出;还需要你这种扩展么?
@Test
public void run32() throws IOException {
String token = UUID.randomUUID().toString();
MDC.put("G_TOKEN", token);
log.debug("token is {}", token);
new Thread(new Runnable() {
@Override
public void run() {
log.debug("thread-2 start");
log.debug("map={}", MDC.getCopyOfContextMap());
new Thread(new Runnable() {
@Override
public void run() {
log.debug("thread-3 start");
log.debug("map={}", MDC.getCopyOfContextMap());
Runnable runable = new Runnable() {
@Override
public void run() {
log.debug("abc");
}
};
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.submit(runable);
}
}).start();
}
}).start();
System.in.read();
MDC.clear();
}
输出
2017/07/20 13:23:15.433 DEBUG [main][7f7f35f0-7adf-4792-810d-0a6c56502b04] com.spy.test.log.MDCTest:39>>token is 7f7f35f0-7adf-4792-810d-0a6c56502b04
2017/07/20 13:23:15.437 DEBUG [Thread-0][7f7f35f0-7adf-4792-810d-0a6c56502b04] com.spy.test.log.MDCTest$1:44>>thread-2 start
2017/07/20 13:23:15.437 DEBUG [Thread-0][7f7f35f0-7adf-4792-810d-0a6c56502b04] com.spy.test.log.MDCTest$1:46>>map={G_TOKEN=7f7f35f0-7adf-4792-810d-0a6c56502b04}
2017/07/20 13:23:15.439 DEBUG [Thread-1][7f7f35f0-7adf-4792-810d-0a6c56502b04] com.spy.test.log.MDCTest$1$1:51>>thread-3 start
2017/07/20 13:23:15.440 DEBUG [Thread-1][7f7f35f0-7adf-4792-810d-0a6c56502b04] com.spy.test.log.MDCTest$1$1:53>>map={G_TOKEN=7f7f35f0-7adf-4792-810d-0a6c56502b04}
2017/07/20 13:23:15.446 DEBUG [pool-2-thread-1][7f7f35f0-7adf-4792-810d-0a6c56502b04] com.spy.test.log.MDCTest$1$1$1:59>>abc
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.