Spring Boot - How can I have different log configurations per profile?

4.5k Views Asked by At

I am trying to configure logging support per different profile in Spring Boot. This is the relevant part of the application.yml file:

spring:
    profiles.active: development
---
spring:
    profiles: development
logging.config: logback-development.xml
---
spring:
    profiles: test
logging.config: logback-test.xml

As you can see I have two profiles (development is the default one) and I'm using Logback as the logging framework. Only difference between two Logback configuration xml files is that test one is logging both to console and a file.

The problem I'm encountering is that the logback-test.xml configuration is used even if I'm using development profile. Maybe I miss-understood Spring Boot configuration, but shouldn't this configuration allow me to use different logging configuration per Spring profile?

logback-development.xml content:

<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="true">
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <charset>utf-8</charset>
            <Pattern>[%p] %c - %m%n</Pattern>
        </encoder>
    </appender>

    <logger name="rs.rmilovic.bookmarksmanager" level="DEBUG" />

    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
    </contextListener>

    <root level="${logback.loglevel}">
        <appender-ref ref="CONSOLE" />
    </root>
</configuration>

logback-test.xml content:

<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="true">
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>logs/holiday_requets.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>90</maxHistory>
        </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>

    <logger name="rs.rmilovic.bookmarksmanager" level="INFO" />

    <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator">
        <resetJUL>true</resetJUL>
    </contextListener>

    <root level="${logback.loglevel}">
        <appender-ref ref="ASYNC" />
    </root>
</configuration>
2

There are 2 best solutions below

5
On

Change the logging.config to look in the classpath like this

logging:
  config: classpath:logback-development.xml
0
On

Instead of *.yml, set it in relevant *.properties file for profile, e.g.: in

application-prod.properties
set:


    logging.config=classpath:logback-prod.xml