启动Spring boot项目报错:java.lang.IllegalArgumentException: LoggerFactory is not a Logback

x33g5p2x  于2022-02-20 转载在 Spring  
字(1.2k)|赞(0)|评价(0)|浏览(290)
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>
  1. 添加对 log4j 的依赖
<!-- 添加 log4j 依赖 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

到此,Spring Boot 项目的日志框架就已经替换成 log4j 了,如需要定义日志输出,只需添加 log4j.properties 文件并在该文件中添加相应配置即可。

相关文章

微信公众号

最新文章

更多