-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
能否提供与LOG4J(2)中的MDC集成或增强 #49
Comments
另外,在 |
@oldratlee 感谢回复。 使用 一般场景可以是:
|
protected void beforeExecute() {
}
protected void afterExecute() {
} 可以覆盖这个方法 来完成 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 输出:
@bwzhang2011 看看这样是不是可以解决你的问题了? 如果OK,我就发一个新版本,加上这个功能。 |
v1.2.0 已发布。 @bwzhang2011 |
@oldratlee, 我再仔细看一下,看是否能合到工程中。T.K.S |
@oldratlee |
相关的
你说能一下 『针对其的实现』如何做,给一下示例代码? 如果你比较清楚了,能直接实现一下,给个 |
很赞 |
赞 |
PS: 已提供集成库: <dependency>
<groupId>com.ofpay</groupId>
<artifactId>logback-mdc-ttl</artifactId>
<version>1.4.0</version>
</dependency> |
Hi~
注意到
multiple-thread-context
提供了上下文线程变量的传递,并提供了针对Runnable
及Callable
的支持。由于上述场景中有一部分是用在日志中,当前log4j(2)
提供了MDC
的支持。想问的是,能否提供一个针对其的增强支持,以
log4j2
为例已经提供了入口操作的类ThreadContext
,若在Executor
中传递则需要 使用 该类中的getContext()
andcloneStack()
方法。此外,框架是否考虑了如何清除这些线程安全变量(以
WEB
为例大多数是在filter
里面使用PUT
然后 执行完毕后POP
或CLEAR
)——是否有好的建议(以multi-thread-context
的使用为例)。The text was updated successfully, but these errors were encountered: