java - Log4j:为什么在提交给 ExecutorService 的 Callable 中登录时缺少日志条目
问题描述
我有一些看起来像这样的东西:
private static final Logger log = Logger.getLogger(thisClass.class);
private static final ExecutorService EXECUTOR = Executors.newFixedThreadPool(1);
...
...
public void method1() {
Callable<Integer> callable = () -> {
Timer timer = new Timer();
try {
Integer i = someMethod();
log.info("got " + i + " in " + timer.getElapsedTime());
} catch (Exception e) {
log.info("exception thrown after " + timer.getElapsedTime());
throw e;
}
return i;
};
method2(callable);
log.info("method1 called method2");
}
public static void method2(Callable<Integer> callable) {
Future<Integer> future = EXECUTOR.submit(task);
future.get();
}
出于某种原因,当我使用 maven 运行我的应用程序时,method1 被调用了 200 次,我可以看到 Callable 之外的日志条目(“method1 调用 method2”)200 次,但我只看到为Callable 中的一个(“在 t 秒内得到 i”)
我究竟做错了什么?
解决方案
使用 AsyncAppender 解决了问题
推荐阅读
- git - Linux 最新稳定编译:不能代表对 vmlinux-gdb.py 的更改:
- javascript - 如何使用 Javascript 拉取 api,对拉取数据进行计算,并将结果发布到网页?
- asp.net-mvc - 未找到框架“.NETFramework,Version=v4.6.1”的参考程序集。尝试使用管道 jenkinsfile 构建时出错
- c - 为什么 unsortedArray 在我的代码运行时会发生变化?
- node.js - 在托管在 S3 上的反应(Web)应用程序中使用类似“aws-sdk”(和 process.env)的安全性如何?
- java - 使用 pdfbox 为文本字段设置工具提示
- c# - 在 C# 中从 Datatable 创建一个深层嵌套对象
- c# - 评估两个对象的更好方法
- tfs - 我的个人机器上有一个构建代理。我可以将代理功能配置为仅构建由我发起的构建,而不是其他开发人员
- c# - 如何从 WebForms 重定向到 MVC 视图?