从同事那接了一个脚手架,启动后发现日志文件没生成。都知道SpringBoot默认带的logback,如果要用log4j需要把logback的依赖去掉。看了下工程的pom.xml文件,确实是有加去掉spring-boot-starter-logging的声明。
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
那怎么就死活不生成日志呢。倒腾了好几次,无意中看到控制台最上面的输出:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/Users/xxx/.m2/repository/ch/qos/logback/logback-classic/1.1.11/logback-classic-1.1.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/Users/xxx/.m2/repository/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.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]
说的是SLF4J依赖冲突,然后最后用例logback的。摔,难怪log4j日志一直没生成。谷歌了一下,找到了解决办法。修改pom.xml,加入以下代码把logback的依赖去掉。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<artifactId>logback-classic</artifactId>
<groupId>ch.qos.logback</groupId>
</exclusion>
<exclusion>
<artifactId>log4j-over-slf4j</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
参考链接:
https://stackoverflow.com/questions/33071002/spring-boot-multiple-slf4j-bindings/33962453