Comments (10)
很赞
from transmittable-thread-local.
赞
from transmittable-thread-local.
LOG4J(2)
的集成 想法不错,我理解和思考一下你说的实现过程。
另外,在MDC
的需求说明中,关于日志场景的说明还是比较模糊的,你能给一下更明确实际些的场景说明不?这样做起来更有目标感 😸 @bwzhang2011
from transmittable-thread-local.
@oldratlee 感谢回复。
使用MDC
是用来记录跟应用上下文环境中需要保存的一些数据,比如请求类提取的、跟执行有关的跟踪信息还有当异常发生的时候提取的,这些可以在appender
中的layout
来指定这些信息(比如%X{key1}: %X{key2}
等)。
一般场景可以是:
filter
类中写入parent thread
的内容(MDC.put
)——带有一traceId
或其余标记信息(用户)- 业务类中写入业务类的信息(
MDC.put
)——可提取跟traceId
或标记有关的信息(提取),并保存其余的消息(跟本线程有关的)并进行保存保存 - 清空
filter
的线程变量,finally { ThreadContext.clearAll() }
from transmittable-thread-local.
MtContextThreadLocal
新加上callback
方法:
protected void beforeExecute() {
}
protected void afterExecute() {
}
可以覆盖这个方法 来完成 log4j2
的设置,达到filter
的效果:
static MtContextThreadLocal<Map<String, String>> mtc = new MtContextThreadLocal<Map<String, String>>() {
@Override
protected void beforeExecute() {
final Map<String, String> log4j2Context = get();
for (Map.Entry<String, String> entry : log4j2Context.entrySet()) {
ThreadContext.put(entry.getKey(), entry.getValue());
}
}
@Override
protected void afterExecute() {
ThreadContext.clearAll();
}
@Override
protected Map<String, String> initialValue() {
return new HashMap<String, String>();
}
};
Log的代码:
public static void main(String[] args) throws Exception {
// Init Log Context, set MTC
// More KV if needed
final String TRACE_ID = "trace-id";
final String TRACE_ID_VALUE = "XXX-YYY-ZZZ";
ThreadContext.put(TRACE_ID, TRACE_ID_VALUE);
mtc.get().put(TRACE_ID, TRACE_ID_VALUE);
// Log in Main Thread
logger.info("Log in main!");
// Run task in thread pool
final ExecutorService executorService = Executors.newFixedThreadPool(1);
final Runnable task = new Runnable() {
@Override
public void run() {
// Log in thread pool
logger.info("Log in Runnable!");
}
};
final Future<?> submit = executorService.submit(MtContextRunnable.get(task));
submit.get();
executorService.shutdown();
}
完整示例:Log4j2ContextFilter.java 、 log4j2.xml
输出:
22:50:52.823 [main] INFO log4j2.Log4j2ContextFilter XXX-YYY-ZZZ - Log in main!
22:50:52.826 [pool-2-thread-1] INFO log4j2.Log4j2ContextFilter XXX-YYY-ZZZ - Log in Runnable!
@bwzhang2011 看看这样是不是可以解决你的问题了? 如果OK,我就发一个新版本,加上这个功能。
from transmittable-thread-local.
v1.2.0 已发布。 @bwzhang2011
from transmittable-thread-local.
@oldratlee, 我再仔细看一下,看是否能合到工程中。T.K.S
from transmittable-thread-local.
@oldratlee SL4J
中MDC
类依赖于MDCAdapter
,个人感觉如果集成则需要提供一个针对其的实现。这样我仍然只需要操作MDC
即可。
from transmittable-thread-local.
相关的 SL4J
的MDC
和 multiple-thread-context
的集成,涉及如果设置业务的Log Context
如trace-id
,这个需要业务来完成, multi-thread-context
提供能力。
个人感觉如果集成则需要提供一个针对其的实现
你说能一下 『针对其的实现』如何做,给一下示例代码?
如果你比较清楚了,能直接实现一下,给个PullRequest
吗? 😸 @bwzhang2011
from transmittable-thread-local.
PS: 已提供集成库:
<dependency>
<groupId>com.ofpay</groupId>
<artifactId>logback-mdc-ttl</artifactId>
<version>1.4.0</version>
</dependency>
from transmittable-thread-local.
Related Issues (20)
- agent 2.14.3 JDK17 报错 HOT 5
- -javaagent:D:\jar\transmittable-thread-local-2.14.0.jar 必须使用带版本号的名称,去除版本号会报错 HOT 1
- k8s环境下CPU调整>2数量后线程上下文传递失效 HOT 1
- 官方maven仓库的版本没有同步? HOT 2
- ListenableFuture添加了监听回调,回调线程获取不到正确的值? HOT 3
- 配合线程池使用时,remove了之后,下一个任务还是能拿到上一个任务设置的值 HOT 1
- 线上运行开始父子线程可以传递threadlocal变量,运行一段时间失效 HOT 1
- 使用jrebel -javaagent方式启动,在stream().parallel()无法正确获取内容
- jdk并发流parallelStream支持这种改造吗 HOT 1
- 在Java21的虚拟线程中不使用`TTL`会有问题吗? HOT 1
- TtlRunnable是否可以支持PriorityQueue和DelayQueue HOT 2
- 从哪里可以看到每个版本迭代哪些内容,或者修复了哪些bug HOT 1
- `Stream#parallel()#forEach()`或者`List#parallelStream()#forEach()`时,如何使用不丢失`TTL`值 HOT 1
- TTL是否支持调用中包含多种协议场景情况下的上下文传递 HOT 1
- TTL Agent instrument the class exception HOT 2
- graalvm TraversingBlockingQueue ArrayStoreException HOT 1
- 父线程调用remove清除缓存后,是否需要子线程显式romove HOT 1
- 请问下,在spring boot 场景下,filter 中进入时set ,但是业务中异步线程还未执行完,filter 请求已经finally remove了,这时获取到的值为null, 这种情况如何保证正确性 HOT 3
- TTL 的 java agent 模式与dragonwell 的 eagerappcds有冲突 HOT 5
- TTL 在ThreadPoolExecutor传递错误(openJ9 agent使用方式) HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from transmittable-thread-local.