SpringBoot使用logback自定义配置时遇到的坑 --- 在 /tmp目录下自动生成spring.log文件

SpringBoot项目使用logback自定义配置后,会在/tmp/ 目录下生成 spring.log的文件(如下图所示)。

SpringBoot使用logback自定义配置时遇到的坑 --- 在 /tmp目录下自动生成spring.log文件

解决方案 通过各种资料的搜索,最终发现问题的所在(logback配置如下)。 <?xml version="1.0" encoding="UTF-8"?> <configuration scan="true"> <include resource="org/springframework/boot/logging/logback/base.xml"/> <springProperty scope="context" source="spring.application.name"/> <property value="logs/${springAppName}"/>​ <appender> <layout> <pattern> %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable </pattern> </layout> </appender> <appender> <rollingPolicy> <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <maxHistory>90</maxHistory> <timeBasedFileNamingAndTriggeringPolicy> <maxFileSize>200MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <charset>utf-8</charset> <pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern> </encoder> </appender> <appender> <queueSize>512</queueSize> <appender-ref ref="FILE"/> </appender> <root level="DEBUG"> <appender-ref ref="CONSOLE"/> <appender-ref ref="ASYNC"/> </root> <!-- https://logback.qos.ch/manual/configuration.html#shutdownHook and https://jira.qos.ch/browse/LOGBACK-1090 --> <shutdownHook/> <contextListener> <resetJUL>true</resetJUL> </contextListener> </configuration> 通过分析上面配置发现base.xml中包含了以下内容: <?xml version="1.0" encoding="UTF-8"?> <!-- Base logback configuration provided for compatibility with Spring Boot 1.1 --> <included> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <property value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml" /> <include resource="org/springframework/boot/logging/logback/file-appender.xml" /> <root level="INFO"> <appender-ref ref="CONSOLE" /> <appender-ref ref="FILE" /> </root> </included>

从base.xml中可以看出来,会在/tmp/目录下默认生成一个spring.log,问题已经发现了,那就说明这里的配置需要更改,改成什么呢?
通过在github上查询,只需要将

<include resource="org/springframework/boot/logging/logback/base.xml"/>

替换成

<include resource="org/springframework/boot/logging/logback/defaults.xml" />

即可。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyxjzg.html