1. 程式人生 > >Log4j2 - java.lang.NoSuchMethodError: com.lmax.disruptor.dsl.Disruptor

Log4j2 - java.lang.NoSuchMethodError: com.lmax.disruptor.dsl.Disruptor

問題

專案使用了log4j2,由於使用了全域性非同步列印日誌的方式,還需要引入disruptor的依賴,最後使用的log4j2和disruptor的版本依賴如下:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.1</version>
</dependency>
<!-- log4j2 AsyncLogger need disruptor-->
<dependency>
    <groupId>com.lmax</groupId>
    <artifactId>disruptor</artifactId>
    <version>3.2.0</version>
</dependency>

在專案最開始的地方(第一次使用到log4j2的logger之前),通過程式碼來啟用全域性非同步列印日誌:

// use asyncLogger for log4j2 framework
System.setProperty("Log4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");

然而在啟動專案後報錯如下:

java.lang.NoSuchMethodError: com.lmax.disruptor.dsl.Disruptor.<init>(Lcom/lmax/disruptor/EventFactory;ILjava/util/concurrent/ThreadFactory;Lcom/lmax/disruptor/dsl/ProducerType;Lcom/lmax/disruptor/WaitStrategy;)V
        at org.apache.logging.log4j.core.async.AsyncLoggerDisruptor.start(AsyncLoggerDisruptor.java:97)
        at org.apache.logging.log4j.core.async.AsyncLoggerContext.start(AsyncLoggerContext.java:75)
    at .......

解決方法

該問題是因為Disruptor的版本較低導致,將版本改用較新版本的即可:

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