我想直接用例子来解释我的问题。如果还需要一些其他信息,请添加注释。
我有一个小应用程序(SpringBoot+Java),它使用ThreadPool执行几个任务。假设我的池中有2个线程。现在,log4j用于日志记录。它将所有线程的日志写入到单个文件中,并且所有2个线程的日志数据是交错的。
所以每个线程都是这样工作的-
[Thread-1][08:15:00] entered into application
[Thread-1][08:15:00] entered method A
[Thread-1][08:15:05] exit method A
[Thread-1][08:15:10] entered method B
[Thread-1][08:15:15] exit method B
[Thread-1][08:15:30] exit from application
[Thread-2][08:15:01] entered into application
[Thread-2][08:15:01] entered method A
[Thread-2][08:15:04] exit method A
[Thread-2][08:15:11] entered method B
[Thread-2][08:15:12] exit method B
[Thread-2][08:15:22] exit from application但是我的日志文件有这些基于时间戳的条目-
[Thread-1][08:15:00] entered into application
[Thread-1][08:15:00] entered method A
[Thread-2][08:15:01] entered into application
[Thread-2][08:15:01] entered method A
[Thread-2][08:15:04] exit method A
[Thread-1][08:15:05] exit method A
[Thread-1][08:15:10] entered method B
[Thread-2][08:15:11] entered method B
[Thread-2][08:15:12] exit method B
[Thread-2][08:15:14] exit from application
[Thread-1][08:15:15] exit method B
[Thread-1][08:15:30] exit from application我希望日志文件打印线程方式的执行,而不是时间戳方式的执行。我希望我的日志文件在其他线程启动之前完全打印每个线程。我怎样才能做到这一点。
我已经搜索并找到了下面的文章作为最近的,但我不想要不同的日志文件- How to log multiple threads in different log files?
我需要将所有日志数据输出到单个日志文件中,按线程名称分组,并按线程开始时间排序。
发布于 2019-03-27 05:00:45
因为我猜在控制台附加器中分离日志是行不通的,所以我建议为每个Thread定义单独的FileAppenders,就像描述here一样
https://stackoverflow.com/questions/55365888
复制相似问题