Springboot启动logback与slf4j的jar冲突

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/x4609883/article/details/80842155

使用Maven管理SpringBoot项目,启动的时候遇到异常:

Exception in thread "main" java.lang.IllegalArgumentException: LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation 

该异常的原因是Springboot本身使用logback打印日志,但是项目中其他的组件依赖了slf4j,这就导致了logback与slf4j的jar包之间出现了冲突,这种情况下,有两种解决方式:

1,去除slf4j,继续使用lagback

    这种方式需要在pom.xml文件中找到所有依赖了slf4j的组件,在<dependency>中使用<exclusions>排除slf4j的依赖

2,去除logback,改为使用slf4j

    pom.xml去掉,同时去掉没有用的配置文件logback-spring.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                  <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
</exclusions>
</dependency>

使用log4j或者slf4j的方式打印日志,别忘记了配置文件log4j.properties

具体选择那种方式要根据少数服从多数的原则,如果很多组件都依赖了slf4j,那么就选择使用slf4j,去掉logback

相反,如果自有个别组件依赖了slf4j,可以排除slf4j,继续使用logback。

猜你喜欢

转载自blog.csdn.net/x4609883/article/details/80842155