Log4j2 log asynchronous printing (example explanation)

Log4j2 supports asynchronous log printing. The advantage of asynchronous log output is that using a separate process to perform log printing can improve log execution efficiency and reduce the impact of log function on normal business.

The asynchronous log needs to load disruptor-3.0 in the classpath of the program 0.jar or higher.

Asynchronous loggers is a new feature. In log4j 2, it can be fully asynchronous or mixed with synchronization. It can also only asynchronize the appender to improve the system performance. Official data show that mixing is not as effective as full asynchronization.

1. Fully asynchronous mode:

This asynchronous logging method does not need to modify the original configuration file. The logger still uses < root > and < logger >

Just add a system attribute code at the beginning of the main program code:

Or set startup parameters:

2. Asynchronous and non asynchronous mixed output mode

In the configuration file, the logger uses < asyncroot > or < asynclogger >, and < asyncroot > or < asynclogger > can be mixed with < root > or < logger >.

It should be noted here that if < asyncroot > or < asynclogger >, includelocation = "true" must be set before some information such as classpath can be printed.

3. Only asynchronize Appenders

Add the following < async > tag to the < Appenders > tag

Then reference asyncappender in the < root > or < logger > tag. Here, includelocation is added to the < async > tag.

Whether in fully asynchronous mode or mixed mode, in the appender tag, whether the immediateflush attribute is true or false, the effect is the same as setting false.

4. Performance comparison

Fully asynchronous > mixed mode > asynchronous only appender > synchronous

5. Questions

When the mixed asynchronous mode is used for multi-threaded log writing test, the log is not written occasionally. Did the main thread stop the process when it finished executing and would not wait for the log writing thread to finish executing? In the last few seconds of the main thread, there will be no more log writing.

The above log4j2 log asynchronous printing (example explanation) is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>