还在用 Log4j ?快用 Log4j2,性能太猛了!

环境

jdk:1.7.0_79

cpu:[email protected] 4核

eclipse:3.7

操作系统:win7

准备

1.log4j:1.7.21


        org.slf4j
    slf4j-log4j12
    1.7.21

log4j.xml





    
        
            
        

        
        
            
            
            
        
    

    
        
        
        
        
            
        
    

    
        
        
    

    
        
         
    

2.logback:1.1.7


    ch.qos.logback
    logback-classic
    1.1.7

logback.xml


    
        
        
            %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
            
        
    

    
        testFile.log
        true
        
            [%t] - %m%n
            
        
    

    
    
        0
        
    

    
          
    

    
        
    

3.log4j2:2.6.2


    org.apache.logging.log4j
    log4j-core
    2.6.2


    org.apache.logging.log4j
    log4j-slf4j-impl
    2.6.2


    com.lmax
    disruptor
    3.3.4

log4j2.xml





    
         
            
        

        
            
                
                
            
            
            
                
                
            
        

        
            
                [%t] - %m%n
            
        

    

    
        
        
            
        
    

测试

准备50条线程同时记录1000000条数据,然后统计时间,详细代码如下:

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() {

                @Override
                public void run() {
                    for (int es = 0; es < everySize; es++) {
                        log.info("======info");
                    }
                    cdl.countDown();
                }
            }).start();
        }

        cdl.await();
        long endTime = System.currentTimeMillis();
        System.out.println("log4j1:messageSize = " + messageSize
                + ",threadSize = " + threadSize + ",costTime = "
                + (endTime - startTime) + "ms");
    }
}

log4j1和logback的同步和异步分别修改为对应的appender就行了

log4j2的异步方式提供了2中模式:

1.全局开启

设置Log4jContextSelector系统属性为:
org.apache.logging.log4j.core.async.AsyncLoggerContextSelector

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

2.混合同步异步模式

不需要设置Log4jContextSelector,但是需要使用AsyncLogger标签

更多详细参考官方文档:http://logging.apache.org/log...

结果
分别测试完以后统计成表格如下:

还在用 Log4j ?快用 Log4j2,性能太猛了!_第1张图片

log4j2的异步模式表现了绝对的性能优势,优势主要得益于Disruptor框架的使用

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.

一个无锁的线程间通信库代替了原来的队列

更多Disruptor :

http://developer.51cto.com/ar...

http://ifeve.com/disruptor/

来源:https://my.oschina.net/OutOfM...

近期热文推荐:

1.1,000+ 道 Java面试题及答案整理(2021最新版)

2.别在再满屏的 if/ else 了,试试策略模式,真香!!

3.卧槽!Java 中的 xx ≠ null 是什么新语法?

4.Spring Boot 2.5 重磅发布,黑暗模式太炸了!

5.《Java开发手册(嵩山版)》最新发布,速速下载!

觉得不错,别忘了随手点赞+转发哦!

你可能感兴趣的:(java)