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" name="springAppName" source="spring.application.name"/>    
  <property name="LOG_FILE" value="logs/${springAppName}"/>​    

  <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">    
    <layout class="ch.qos.logback.classic.PatternLayout">    
      <pattern>    
        %black(%d{ISO8601}) %highlight(%-5level) [%blue(%t)] %yellow(%C{1.}): %msg%n%throwable    
      </pattern>    
    </layout>    
  </appender>    
  <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">    
    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">    
      <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i.log</fileNamePattern>    
      <maxHistory>90</maxHistory>    
      <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">    
        <maxFileSize>200MB</maxFileSize>    
      </timeBasedFileNamingAndTriggeringPolicy>    
    </rollingPolicy>    
    <encoder>    
      <charset>utf-8</charset>    
      <pattern>%d %-5level [%thread] %logger{0}: %msg%n</pattern>    
    </encoder>    
  </appender>    
  <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender">    
    <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 class="ch.qos.logback.core.hook.DelayingShutdownHook"/>    

  <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">    
    <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 name="LOG_FILE" 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" />

即可。

问题解决。

posted @ 2019-02-27 17:10  灬毛毛  阅读(14430)  评论(0编辑  收藏  举报