我们希望在Spring Boot中为不同的配置文件使用不同的日志记录配置,例如在本地运行中,我们只希望控制台日志记录和用于生产,我们希望文件记录日志支持每天滚动日志文件。
我想出了一个示例logback配置,该配置将在所有应用程序中使用。 在src/main/resources
创建一个名为logback-spring.xml
的文件,其内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<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" />
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>
<pattern>${FILE_LOG_PATTERN}</pattern>
</encoder>
<file>${LOG_FILE}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FILE}.%d</fileNamePattern>
</rollingPolicy>
</appender>
<springProfile name="local">
<root level="INFO">
<appender-ref ref="CONSOLE" />
<appender-ref ref="FILE" />
</root>
</springProfile>
<springProfile name="test,prod">
<root level="INFO">
<appender-ref ref="FILE" />
</root>
</springProfile>
</configuration>
我们使用的是Spring Boot提供的默认控制台附加程序,但是提供了我们自己的基于日常滚动的文件附加程序。 我主要复制了base.xml并对其进行了更新以满足我的需求。