log4j日志只打印到控制台,没有输出到日志文件的问题


原因:SLF4J只能绑定一个唯一的日志框架,这里提示绑定了两个,并且列出了位置,只要排除掉一个即可。

项目启动时控制台报错警告信息:

08-Jan-2020 15:01:07.141 警告 [RMI TCP Connection(7)-127.0.0.1] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [amcc-ivr] appears to have started a thread named [commons-pool-EvictionTimer] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread: java.lang.Object.wait(Native Method) java.util.TimerThread.mainLoop(Timer.java:552) java.util.TimerThread.run(Timer.java:505) 08-Jan-2020 15:01:11.262 信息 [RMI TCP Connection(7)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars 至少有一个JAR被扫描用于TLD但尚未包含TLD。 为此记录器启用调试日志记录,以获取已扫描但未在其中找到TLD的完整JAR列表。 在扫描期间跳过不需要的JAR可以缩短启动时间和JSP编译时间。 SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found binding in [jar:file:/D:/MXF/IDEA/amcc/amcc-ivr/target/amcc-ivr/WEB-INF/lib/logback-classic-1.1.5.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in [jar:file:/D:/MXF/IDEA/amcc/amcc-ivr/target/amcc-ivr/WEB-INF/lib/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation. SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]

查看jar包依赖列表,可以看到项目本身依赖的jar没有冲突。但是有可能只是你本身的项目没引用,像我引用了公共的jar,说不定里面就包含了log4j导致了冲突,我的就是这样。可以发现amcc-utils引用了logback-classic。但我本身的项目是使用slf4j-api的。于是产生了冲突。

解决办法是屏蔽掉这个引用:

 

 


<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>

之后Maven重新clean打包下,重启项目,日志文件就正常输出了。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM