Spring Boot logback extensions
Spring Boot adds some useful extensions to Logback to help with advanced configuration.
One of this extensions is the tag <springProfile>
that permits to define a conditional part in the log configuration depending from the active spring profiles.
The only requirement to use these extensions is to name the log configuration file logback-spring.xml
instead of logback.xml
Example
We want to add a rolling file appender only in production, in this scenario the application will use the prod
profile.
The logback-spring.xml
configuration.
<configuration>
<property name="LOG_FOLDER" value="." />
<appender name="Console"
class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{HH:mm:ss.SSS, Europe/Rome} %-5level %logger{0} - %msg%n
</Pattern>
</layout>
</appender>
<springProfile name="prod">
<appender name="RollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_FOLDER}/application.log</file>
<encoder
class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{HH:mm:ss.SSS, Europe/Rome} %-5level %logger{0} - %msg%n</Pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_FOLDER}/application.log.%d{yyyy-MM-dd, Europe/Rome}</fileNamePattern>
<maxHistory>60</maxHistory>
</rollingPolicy>
</appender>
</springProfile>
<!-- LOG everything at INFO level -->
<root level="info">
<springProfile name="prod">
<appender-ref ref="RollingFile" />
</springProfile>
<appender-ref ref="Console" />
</root>
</configuration>
Resources
The related documentation section: https://docs.spring.io/spring-boot/reference/features/logging.html#features.logging.logback-extensions