Printing Thread Info in Log File Using Log4j2 – 使用Log4j2在日志文件中打印线程信息

最后修改: 2022年 2月 6日

1. Overview 


This tutorial will show the idea and examples of logging thread information using the Log4j2 library


2. Logging and Threads


Logs are a powerful tool to provide the context about what was happening in the system when some error or flow occurred. Logging helps us capture and persist relevant information to be analyzed at any time.


Threads allow our application to execute multiple things simultaneously to handle more requests and make our jobs more efficient.


Many Java applications use logging and threads to control their process in this scenario. However, as the logs usually concentrate on a specific file, the logs mess up from different threads, and the user cannot identify and understand the sequence of the events. We will use one of the most popular Java logging frameworks, Log4j2, to show relevant information about our thread to solve this problem.


3. Log4j2 Usage


Forward, we have an example of using some parameters in Log4j2 to show information about our thread:


<Property name="LOG_PATTERN"> %d{yyyy-MM-dd HH:mm:ss.SSS} --- thread_id="%tid" thread_name="%tn" thread_priority="%tp" --- [%p] %m%n </Property>

Log4j2 uses parameters in its pattern to refer to data. All parameters start with a in their beginner. Here are some examples of thread parameters:


  • tid: Thread identifier is a positive long number generated when the thread is created.
  • tn: It’s a sequence of characters that names a thread.
  • tp: Thread priority is an integer number between 1 and 10 where more significant numbers mean higher priority.

First, as it suggests, we are adding the information’s about the id, name, and priority of our thread. Therefore, to visualize it, we need to create a simple application that makes new threads and log some info:


public class Log4j2ThreadInfo{
    private static final Logger logger = LogManager.getLogger(Log4j2ThreadInfo.class);
    public static void main(String[] args) {
        IntStream.range(0, 5).forEach(i -> {
            Runnable runnable = () ->"Logging info");
            Thread thread = new Thread(runnable);

In other words, we are simply running a forEach in a range of 0 to 5 with the help of Java Streams and then starting a new thread with some logging. As a result, we’re going to have:

换句话说,我们只是在Java Streams的帮助下,在0到5的范围内运行一个forEach,然后启动一个新的线程,进行一些记录。结果是,我们会有。

2022-01-14 23:44:56.893 --- thread_id="22" thread_name="Thread-2" thread_priority="5" --- [INFO] Logging info
2022-01-14 23:44:56.893 --- thread_id="21" thread_name="Thread-1" thread_priority="5" --- [INFO] Logging info
2022-01-14 23:44:56.893 --- thread_id="20" thread_name="Thread-0" thread_priority="5" --- [INFO] Logging info
2022-01-14 23:44:56.893 --- thread_id="24" thread_name="Thread-4" thread_priority="5" --- [INFO] Logging info
2022-01-14 23:44:56.893 --- thread_id="23" thread_name="Thread-3" thread_priority="5" --- [INFO] Logging info

4. Conclusion


This article shows a simple way to add thread information in your Java Project using Log4j2 Parameters. If you want to check the code up, it is available over on GitHub.