tomcat启动springboot application提示“LoggerFactory is not a Logback LoggerContext but Logback is on the classpath”

Life is short , play more!
本文来自lihao's Blog,转载请注明。

tomcat 部署springboot application后(打包成war包部署),提示如上错误。根据下面的堆栈错误原因, 可知slf4j 提供的LoggerFactory 并没有使用Logback 上下文, 但是logback的库被引入到了我们的工程里。 这时候springboot 应用启动的时候, spring框架广播了一个事件, LoggingApplicationListener 侦听到了该事件后,启动了检查工作,发现logback并没有启用, 报了如上错误。 查看如此解决办法可以想到有2个办法。 1)  移除logback 包 , 错误里也提示的比较明确, 使用默认的 log4j 管理日志打印。(因为错误已经明确提示告诉我们,org.apache.logging.slf4j.Log4jLoggerFactory 被默认加载了。 ) 2) 移除log4j , 让slf4j 使用logback 上下文。

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:/opt/app/autoenv/tomcat-test-asistor/webapps/ROOT/WEB-INF/lib/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
   

at org.springframework.util.Assert.instanceCheckFailed(Assert.java:389)
        at org.springframework.util.Assert.isInstanceOf(Assert.java:327)
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:274)
        at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:98)
        at org.springframework.boot.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:230)
        at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:209)
        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:122)
        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:292)
        at org.springframework.boot.web.support.SpringBootServletInitializer.run(SpringBootServletInitializer.java:154)
        at org.springframework.boot.web.support.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:134)
        at org.springframework.boot.web.support.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:87)
        at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5573)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
        ... 10 more

 

好的,那么我先尝试移除log4j 。

首先运行 mvn dependency:tree -Dverbose 查看哪些包引入了 log4j或者logback

发现spring-boot-starter-web 里的 spring-boot-starter-logging , 会引入

[INFO] |  |  |  +- ch.qos.logback:logback-classic:jar:1.1.11:compile
[INFO] |  |  |  |  +- ch.qos.logback:logback-core:jar:1.1.11:compile
[INFO] |  |  |  |  \- (org.slf4j:slf4j-api:jar:1.7.25:compile - version managed from 1.7.22; omitted for duplicate)
[INFO] |  |  |  +- org.slf4j:jcl-over-slf4j:jar:1.7.25:compile
[INFO] |  |  |  |  \- (org.slf4j:slf4j-api:jar:1.7.25:compile - version managed from 1.7.22; omitted for duplicate)
[INFO] |  |  |  +- org.slf4j:jul-to-slf4j:jar:1.7.25:compile
[INFO] |  |  |  |  \- (org.slf4j:slf4j-api:jar:1.7.25:compile - version managed from 1.7.22; omitted for duplicate)
[INFO] |  |  |  \- org.slf4j:log4j-over-slf4j:jar:1.7.25:compile
[INFO] |  |  |     \- (org.slf4j:slf4j-api:jar:1.7.25:compile - version managed from 1.7.22; omitted for duplicate)

所以我们exclude log4j-over-slf4j 包,则默认使用 logback 日志框架,  重新编译后, 部署到tomcat启动
		
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
        <exclusion>
            <groupId>org.slf4j</groupId>
            <artifactId>og4j-over-slf4j</artifactId>
        </exclusion>
    </exclusions>
		</dependency>

 如果我们想移除logback, 使用log4j框架,则我们改成如下内容即可。 

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
        <exclusions>
        <exclusion>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
    </exclusions>
		</dependency>

 


发表评论

电子邮件地址不会被公开。 必填项已用*标注