1. Overview
1.概述
This tutorial will show the idea and examples of logging thread information using the Log4j2 library.
本教程将展示使用Log4j2库来记录线程信息的想法和实例。
2. Logging and Threads
2.记录和线程
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.
在这种情况下,许多Java应用程序使用日志和线程来控制其进程。然而,由于日志通常集中在一个特定的文件上,不同的线程的日志混乱不堪,用户无法识别和了解事件的顺序。我们将使用最流行的Java日志框架之一Log4j2,来显示我们线程的相关信息,以解决这个问题。
3. Log4j2 Usage
3.Log4j2的用法
Forward, we have an example of using some parameters in Log4j2 to show information about our thread:
向前,我们有一个例子,在Log4j2中使用一些参数来显示我们线程的信息。
<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:
Log4j2在其模式中使用参数来指代数据。所有的参数都以%开头,在它们的开头。下面是一些线程参数的例子。
- 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:
首先,正如它所暗示的,我们正在添加关于我们线程的ID、名称和优先级的信息。因此,为了使其可视化,我们需要创建一个简单的应用程序,创建新线程并记录一些信息。
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 = () -> logger.info("Logging info");
Thread thread = new Thread(runnable);
thread.start();
});
}
}
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
4.总结
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.
这篇文章展示了一种使用Log4j2参数在Java项目中添加线程信息的简单方法。如果你想查看代码,可以在GitHub上找到。