Springboot启动logback与slf4j的jar冲突

使用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的组件,在中使用排除slf4j的依赖

2,去除logback,改为使用slf4j

    pom.xml去掉,同时去掉没有用的配置文件logback-spring.xml

org.springframework.boot
spring-boot-starter-web

               
                    org.springframework.boot
                  spring-boot-starter-logging
               


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

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

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

你可能感兴趣的:(Springboot)