项目中log系统使用的 logback + slf4j 实现, 今天一同事新引入几个jar包,结果tomcat启动时报了以下异常:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'javaEntranceProxyTypeHolder' defined in class path resource [sentinel-tracer-entrance.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchFieldError: fileNameat org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1514)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)... 18 more
Caused by: java.lang.NoSuchFieldError: fileNameat org.apache.log4j.DailyRollingFileAppender.activateOptions(DailyRollingFileAppender.java:224)at org.apache.log4j.DailyRollingFileAppender.<init>(DailyRollingFileAppender.java:205)at com.taobao.csp.courier.RecordLog.<clinit>(RecordLog.java:42)at com.taobao.csp.sentinel.entrypoint.ProxyTypeHolder.afterPropertiesSet(ProxyTypeHolder.java:44)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1573)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1511)
很奇怪怎么还会报 log4j 的异常, 最后查了包依赖树,发现是新加的jar包把 log4j-over-slf4j 这个包给传递依赖进来了,当有这个包存在时,日志系统会回调到log4j的实现上,所以有了上面这个log4j的异常。
解决方法: 排除传递依赖的 log4j-over-slf4j
打开pom文件的 Dependency Hierarchy模式, 找到是哪个jar引入的这个包,把它里面的log4j-over-slf4j这个包排除掉即可,加上下面配置:
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
</exclusion>
</exclusions>
更多文章:http://www.tuicool.com/articles/INveIf