Springboot工程logback和log4j冲突问题的解决。

背景 

     刚入职,最近搭建一个工程。搭建过程需要引入公司已经封装好的一些jar包。引入jar后启动就会报错。报错信息如下

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: 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:/Users/dxm/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.11.2/log4j-slf4j-impl-2.11.2.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
	at org.springframework.util.Assert.instanceCheckFailed(Assert.java:655)
	at org.springframework.util.Assert.isInstanceOf(Assert.java:555)
	at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:286)
	at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:102)
	at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:220)
	at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:199)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
	at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127)
	at org.springframework.boot.context.event.EventPublishingRunListener.starting(EventPublishingRunListener.java:69)
	at org.springframework.boot.SpringApplicationRunListeners.starting(SpringApplicationRunListeners.java:48)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:302)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
	at com.dxm.fbi.Application.main(Application.java:21)
	... 5 more

看日志是logback和log4j的冲突,但是具体冲突在哪不知道,怎么解决也不是很清楚。所以记录一下第一次解决jar冲突的步骤,希望对同样问题的同学有所帮助。如果有大神有更好的解决方法,欢迎留言。感谢感谢。

1 首先熟悉Spring对于log的基本架构。

Spring集成的log系统目前分为slf4j和commonlogging ,这两个相当于api接口层。具体实现是log4j和logback 想要具体了解的可以查看这一套的历史渊源。

2 冲突的原因:

(1)如果依赖的两个jar里面分别用的是log4j和logback。这个时候就会有冲突。

(2)如果依赖的两个jar里面都用了同一样的log4j 或者logback 但是版本不一样。那么也会冲突。

3 解决思路:可能是比较笨的方法。

 3.1 首先要知道自己的用的日志框架。

 我这边用的是log4j,而且只是因为增加了一个依赖就报错,那么要么是版本问题,要么是公司封装的logback。看日志,应该是logback的问题。

LoggerFactory is not a Logback LoggerContext but Logback is on the classpath. Either remove Logback or the competing implementation 

3.2 寻找到冲突的jar

 首先通过日志查看基本明白是因为logback的引入,导致的冲突。所以从pom文件中找到冲突的jar

3.2.1 打开pom文件,右键--> Diagrams-->show dependencies 打开依赖的所有jar

3.2.2 commad+f 输入logback 找到与logback相关的jar。随便点一个查看依赖关系。

3.2.3 根据依赖关系,(箭头指向)找到是不是新添加的jar依赖了logback 。

3.3 排除掉新增加的依赖的jar中logback

到此为止,logback和log4j的冲突已经解决,再次在pom的diagrams中查找已经找不到logback相关的jar包。程序也不在报错。

方法有些许笨重,如果小伙伴有好的解决方法欢迎留言。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值