Gangmax Blog

自由之思想,独立之精神

Spring Boot logging at "/tmp"

Today I find an issue when running a Spring Boot web application: the web app generates logging files at the “/tmp” directory which consumes a lot of storage. I didn’t set this. Why?

The answer is here.

The reason is that, the “logback.xml” file I used has the following content which introduces some default logging behaviors:

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

Source code of the “base.xml” file can be found here. It looks like below:

base.xml
1
<?xml version="1.0" encoding="UTF-8"?>
2
3
<!--
4
Base logback configuration provided for compatibility with Spring Boot 1.1
5
-->
6
7
<included>
8
	<include resource="org/springframework/boot/logging/logback/defaults.xml" />
9
	<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/>
10
	<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
11
	<include resource="org/springframework/boot/logging/logback/file-appender.xml" />
12
	<root level="INFO">
13
		<appender-ref ref="CONSOLE" />
14
		<appender-ref ref="FILE" />
15
	</root>
16
</included>

Note the “file-appender.xml” file in it, which has the following content:

1
<?xml version="1.0" encoding="UTF-8"?>
2
3
<!--
4
File appender logback configuration provided for import, equivalent to the programmatic
5
initialization performed by Boot
6
-->
7
8
<included>
9
	<appender name="FILE"
10
		class="ch.qos.logback.core.rolling.RollingFileAppender">
11
		<encoder>
12
			<pattern>${FILE_LOG_PATTERN}</pattern>
13
		</encoder>
14
		<file>${LOG_FILE}</file>
15
		<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
16
			<cleanHistoryOnStart>${LOG_FILE_CLEAN_HISTORY_ON_START:-false}</cleanHistoryOnStart>
17
			<fileNamePattern>${ROLLING_FILE_NAME_PATTERN:-${LOG_FILE}.%d{yyyy-MM-dd}.%i.gz}</fileNamePattern>
18
			<maxFileSize>${LOG_FILE_MAX_SIZE:-10MB}</maxFileSize>
19
			<maxHistory>${LOG_FILE_MAX_HISTORY:-7}</maxHistory>
20
			<totalSizeCap>${LOG_FILE_TOTAL_SIZE_CAP:-0}</totalSizeCap>
21
		</rollingPolicy>
22
	</appender>
23
</included>

Now it’s clear: “base.xml” contains a file appender which will write log to the “/tmp” directory. So I update my “logback.xml” file, replace the “base.xml” file with the “defaults.xml” file:

1
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
2
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
3
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
4
    <!-- This is the file appender I defined. -->
5
</appender>
6
<root level="INFO">
7
    <appender-ref ref="CONSOLE" />
8
    <appender-ref ref="FILE"/>
9
</root>

Note that I still keep the console appender defined by Sprint Boot, since I still want to keep the logging console output.

Comments