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

一世执手 提交于 2019-11-26 16:12:00

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 文件并在该文件中添加相应配置即可。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!