啟動Spring boot項目報錯:java.lang.IllegalArgumentException: LoggerFactory is not a Logback


java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation (class org.apache.logging.slf4j.Log4jLoggerFactory loaded from file:/D:/develop/apache-maven-3.5.3/repo/org/apache/logging/log4j/log4j-slf4j-impl/2.7/log4j-slf4j-impl-2.7.jar). If you are using WebLogic you will need to add 'org.slf4j' to prefer-application-packages in WEB-INF/weblogic.xml:

org.apache.logging.slf4j.Log4jLoggerFactory

解決:

在 Spring Boot 中,程序默認使用 Logback 來記錄日志並用 INFO 級別輸出到控制台,某些情況下我們可能想用其他日志實現框架替換 Logback,在 Spring Boot 中,因為程序使用了自動配置,所以我們可以很方便地替換日志實現,下面以使用 log4j 替換 Logback 為例。

1. 去除對默認日志的依賴

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

2. 添加對 log4j 的依賴

<!-- 添加 log4j 依賴 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

到此,Spring Boot 項目的日志框架就已經替換成 log4j 了,如需要定義日志輸出,只需添加 log4j.properties 文件並在該文件中添加相應配置即可。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM