Still using log4j? Use log4j2 quickly, the performance is too fierce!

Java technology stack 2021-09-15 10:41:51

Environmental Science


cpu:i5-4570@3.20GHz 4 nucleus


operating system :win7

Get ready




<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j=''>
<appender name="myConsole" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" />
<!-- The filter sets the level of output -->
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="debug" />
<param name="levelMax" value="warn" />
<param name="AcceptOnMatch" value="true" />
<appender name="myFile" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="log4jTest.log" />
<param name="Append" value="true" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%t] - %m%n" />
<appender name="async_file" class="org.apache.log4j.AsyncAppender">
<param name="BufferSize" value="32" />
<appender-ref ref="myFile" />
<logger name="org.logTest" additivity="false">
<level value="info" />
<appender-ref ref="async_file" /> <!-- Sync :FILE asynchronous :async_file -->




<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<!-- encoder Default configuration is PatternLayoutEncoder -->
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<pattern>[%t] - %m%n
<!-- Asynchronous output -->
<appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">
<appender-ref ref="FILE" />
<logger name="org.logTest" level="INFO"
<appender-ref ref="ASYNC" /> <!-- Sync :FILE asynchronous :ASYNC-->
<root level="ERROR">
<appender-ref ref="STDOUT" />




<?xml version="1.0" encoding="UTF-8"?>
<!-- Set up log4j2 Of itself log The level of warn -->
<configuration status="warn">
<console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n" />
<RollingFile name="RollingFileInfo" fileName="info.log"
<ThresholdFilter level="INFO" />
<ThresholdFilter level="WARN" onMatch="DENY"
onMismatch="NEUTRAL" />
<PatternLayout pattern="[%t] - %m%n" />
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="100 MB" />
<RandomAccessFile name="RandomAccessFile" fileName="asyncWithLocation.log"
immediateFlush="false" append="true">
<Pattern>[%t] - %m%n</Pattern>
<!-- <AsyncLogger name="asynLogger" level="trace"
<AppenderRef ref="RandomAccessFile" />
</AsyncLogger> -->
<Root level="info" includeLocation="true">
<AppenderRef ref="RollingFileInfo" />


Get ready 50 All threads record at the same time 1000000 Data , Then count the time , The detailed code is as follows :

import java.util.concurrent.CountDownLatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class App {
private static Logger log = LoggerFactory.getLogger(App.class);
public static void main(String[] args) throws InterruptedException {
int messageSize = 1000000;
int threadSize = 50;
final int everySize = messageSize / threadSize;
final CountDownLatch cdl = new CountDownLatch(threadSize);
long startTime = System.currentTimeMillis();
for (int ts = 0; ts < threadSize; ts++) {
new Thread(new Runnable() {
public void run() {
for (int es = 0; es < everySize; es++) {"======info");
long endTime = System.currentTimeMillis();
System.out.println("log4j1:messageSize = " + messageSize
+ ",threadSize = " + threadSize + ",costTime = "
+ (endTime - startTime) + "ms");

log4j1 and logback The synchronization and asynchrony of are modified to the corresponding appender That's it

log4j2 The asynchronous mode of provides 2 Middle mode :

1. Global on

Set up Log4jContextSelector The system attribute is :

System.setProperty("Log4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");

2. Mixed synchronous asynchronous mode

You don't have to set it Log4jContextSelector, But it needs to be used AsyncLogger label

Refer to the official documentation for more details :

After each test, the statistics are as follows :

log4j2 The asynchronous mode of shows an absolute performance advantage , The advantage is mainly due to Disruptor Use of framework

LMAX Disruptor technology. Asynchronous Loggers internally use the Disruptor, a lock-free inter-thread communication library, instead of queues, resulting in higher throughput and lower latency.

A lockless inter thread communication library replaces the original queue

more Disruptor :

source :

Recent hot article recommends :

1.1,000+ Avenue Java Arrangement of interview questions and answers (2021 The latest version )

2. Don't fill the screen again if/ else 了 , Try strategy mode , It's delicious !!

3. Oh my god !Java Medium xx ≠ null What is the new grammar ?

4.Spring Boot 2.5 Blockbuster released , Dark mode is too explosive !

5.《Java Development Manual ( Song Mountain version )》 The latest release , Download it quickly !

I think it's good , Don't forget to like it + Forward !

Please bring the original link to reprint ,thank
Similar articles