英文:
Log4j ThreadContext MultiThread won't inherit
问题
我遇到了这个帖子的问题
以下是我的代码示例:
public class StackOverflow implements RequestHandler<Map<String, String>, ApiGatewayResponse> {
private static final Logger LOGGER = LogManager.getLogger(StackOverflow.class);
public abstract class StreamGobbler extends Thread {
// 使用this.print("message")的工作拦截器
public abstract void print(String line);
}
private class ErrStreamGobbler extends StreamGobbler {
ErrStreamGobbler(InputStream in) { super(in); }
@Override
public void print(String line) { LOGGER.error(line); }
}
private class OutStreamGobbler extends StreamGobbler {
OutStreamGobbler(InputStream in) { super(in); }
@Override
public void print(String line) { LOGGER.info(line); }
}
// 主方法
@Override
public ApiGatewayResponse handleRequest(Map<String, String> params, Context context) {
// ThreadContext 传播
System.setProperty("isThreadContextMapInheritable", "true");
ThreadContext.put("foo", "bar");
LOGGER.info("Hello from main");
// 我的进程将在 System.in 和 System.out 上返回值
ProcessBuilder pb = new ProcessBuilder("sh", String.format("batchs/JOB_FOO/run.sh"));
Process p = pb.start();
// 拦截这些日志
new ErrStreamGobbler(p.getErrorStream()).start();
new OutStreamGobbler(p.getInputStream()).start();
p.waitFor();
}
}
LOGGER.info("Hello from main");
中的 ThreadContext
是有效的,并且还会打印出 foo: bar
。
但是我的子线程 StreamGobbler
并未获得 ThreadContext
,即使将 isThreadContextMapInheritable
设置为 true
,也不会将 foo: bar
打印为 log4j 属性。
英文:
I encounter this post's problem
Here my sample of code
public class StackOverflow implements RequestHandler<Map<String, String>, ApiGatewayResponse> {
private static final Logger LOGGER = LogManager.getLogger(StackOverflow.class);
public abstract class StreamGobbler extends Thread {
// Working Interceptor using this.print("message")
public abstract void print(String line);
}
private class ErrStreamGobbler extends StreamGobbler {
ErrStreamGobbler(InputStream in) { super(in); }
@Override
public void print(String line) { LOGGER.error(line); }
}
private class OutStreamGobbler extends StreamGobbler {
OutStreamGobbler(InputStream in) { super(in); }
@Override
public void print(String line) { LOGGER.info(line); }
}
// MAIN METHOD
@Override
public ApiGatewayResponse handleRequest(Map<String, String> params, Context context) {
// ThreadContext propagation
System.setProperty("isThreadContextMapInheritable", "true");
ThreadContext.put("foo", "bar");
LOGGER.info("Hello from main");
// My process will return value on System.in & System.out
ProcessBuilder pb = new ProcessBuilder("sh", String.format("batchs/JOB_FOO/run.sh"));
Process p = pb.start();
// Intercept those logs
new ErrStreamGobbler(p.getErrorStream()).start();
new OutStreamGobbler(p.getInputStream()).start();
p.waitFor();
}
}
The ThreadContext
from LOGGER.info("Hello from main");
does work and also prints foo: bar
.
But my child threads StreamGobbler
doesn't get ThreadContext
and won't print foo: bar
as a log4j property event if isThreadContextMapInheritable
is set to true
.
答案1
得分: 2
刚刚遇到了这个问题。发现在应用程序启动时设置 -DisThreadContextMapInheritable=true,即使在代码中设置它并不起作用。
英文:
Just encountered this problem. Found setting -DisThreadContextMapInheritable=true at application startup works, even though setting it in the code does not.
答案2
得分: 0
使用自定义的ThreadPoolExecutor
确实解决了我的问题。
在这个答案中找到了这个解决方案。
英文:
Using a custom ThreadPoolExecutor
did fix my problem.
Found this solution using this answer.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论