Springboot configuration log4j2 asynchronous output log

Before the strong brother had sent an article: chat log configuration project , which tells the story of the project log4j logging configuration file, save the log stage by modifying some parameters to achieve and limit the number of save files related issues and corresponding treatment . Of course, if it is recommended that the project is too large log access ELK log into the search engine query processing later.

Today we still say something about the log, however, is not about saving the log, but about the experience required in the program are great when multiple output log and the log of each volume, how we can deal with in a project to improve the program performance.

When the program requires complex logic output point when a lot of log (for troubleshooting); a plurality of points when the program needs to output a log, and the log output the content of each dot is large; large when the user accesses the application, resulting in high concurrency log when the output is very fast; when the pressure measured QA program you feel poor performance ......

To improve the performance of our programs, taking into account the output of the log and thus will not have a greater impact on the business logic, no doubt, we have to do is log output spun off from the business logic, the more accurate will be through asynchronous manner, so that the log output to minimize the impact on the business, while not affecting the output and save us the necessary logs.

Asynchronous log output:

Then we will simply explain, in Springboot project ( using log4j2 ), how to configure the asynchronous log on global as well as how to verify that the program really opened the asynchronous log output.

Why use log4j2 first to mention it, because in log4j version, functional problems asynchronous log output is still relatively more, the performance is also a lot of difference, today issued another article will show the differences between the two. So, in 0202 in the moment, it is recommended that you be able to upgrade to log4j log4j2. Of course, if you are using logback logging tool, it also opened the way with asynchronous log output, we are interested can own Baidu.

In fact Springboot configuration log4j2 asynchronous log output is relatively simple, there is a mixed-mode and full mode, the government has also recommended a full comparison of the asynchronous mode, the maximum increase performance. So strong brother here, there is only mention the whole configuration asynchronous mode:

A step of adding dependency:

<dependency>
  <groupId>com.lmax</groupId>
  <artifactId>disruptor</artifactId>
  <version>3.4.2</version>
</dependency>

Step two, or added to modify the startup class attribute parameters in the sentence start of the project:

Modify the startup categories:


@SpringBootApplication
public class TestApplication {
public static void main(String[] args) {
//下面语句使得日志输出使用异步处理,减小输出日志对性能的影响
        System.setProperty("Log4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
        SpringApplication.run(TestApplication.class, args);
    }
}

 Modify the project start statement:

java -jar -DLog4jContextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector xxx.jar

Select one of two ways on the line. According to personal preference processing.

Yes, it is a simple two step above can be achieved after the project started, asynchronous log function. Of course, some people might also ask: configuration is configuration, but in the end is not really asynchronous log output of it? How to verify it?

Here, we can check whether it is configured by the success of the thread name to view the project started. The following is a strong brother after starting the project, the project thread situation screenshot:

You can see, there are two threads about Log4j2 where Log4j2-TF-AsyncLogger above the red box is the asynchronous log output thread. It shows that we see the configuration is successful. If you're still unsure, you can run the project in Debug mode, select the thread, ide will enter to view the program reaches the thread of the thread of a series of operations, which will be able to know to successfully configure the asynchronous log output function.

Well, the above is relevant to achieve Springboot achieve log4j2 asynchronous output logs. In fact, the content is relatively simple, but strong brother more want to pass that when we encounter problems, be able to think of appropriate solutions for local projects is not necessary but it did impact performance and business logic, entirely through asynchronous or split way to guarantee performance.

There is not too much to explain the principle, but because the corresponding principle of online help is really too much, but how to configure realize they are nothing but vague, we are interested in principle, also can get to know yourself more, here only serve as a stimulus friends, just hope that we encountered a log-related bottlenecks can think of this solution.

No. get more public attention, there are problems in public may ask questions Oh No:

Strong brother hundred thousand forced hundred

Hundred thousand forced hundred programming, Internet and nothing new insights

Published 54 original articles · won praise 69 · Views 250,000 +

Guess you like

Origin blog.csdn.net/seanxwq/article/details/103977487