Spring Boot, logback and logging.config property

I found a solution and I understood why Spring doesn’t use my logging.config property defined in the application.properties file.

Solution and explanation

When initializing logging, Spring Boot only looks in classpath or environment variables.

The solution I used was to include a parent logback.xml file that included the right logging config file according to the Spring profile.

logback.xml

<configuration>
    <include resource="logback-${spring.profiles.active}.xml"/>
</configuration>

logback-[profile].xml (in this case, logback-dev.xml) :

<included>

    <!-- put your appenders -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
     ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
       <encoder>
           <pattern>%d{ISO8601} %p %t %c{0}.%M - %m%n</pattern>
           <charset>utf8</charset>
        </encoder>
    </appender>

    <!-- put your loggers here -->
    <logger name="org.springframework.web" additivity="false" level="INFO">
        <appender-ref ref="CONSOLE" />
    </logger>

    <!-- put your root here -->
    <root level="warn">
        <appender-ref ref="CONSOLE" />
    </root>

</included>

Note

spring.profiles.active has to be set in command line arguments when starting the app.
Example for JVM properties: -Dspring.profiles.active=dev


Reference documentation


Edit (multiple active profiles)

In order to avoid multiple files, we could use conditional processing which requires Janino dependency (setup here), see conditional documentation.

With this method, we can also check for multiple active profiles at the same time. E.g (I did not test this solution, so please comment if it does not work):

<configuration>

    <if condition='"${spring.profiles.active}".contains("profile1")'>
        <then>
         <!-- do whatever you want for profile1 -->
        </then>
    </if>

    <if condition='"${spring.profiles.active}".contains("profile2")'>
        <then>
         <!-- do whatever you want for profile2 -->
        </then>
    </if>

    <!-- common config -->

</configuration>

See @javasenior answer for another example of a conditional processing.

Leave a Comment