spring boot, logback and logging.config property

116,159

Solution 1

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. E.G for JVM properties : -Dspring.profiles.active=dev

Ref docs :

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.

Solution 2

Another approach that could handle multiple profiles is to create a separate properties file for each environment.

application-prod.properties

logging.config=classpath:logback-prod.xml

application-dev.properties

logging.config=classpath:logback-dev.xml

application-local.properties

logging.config=classpath:logback-local.xml

BE AWARE

If you aren't careful you could end up logging somewhere unexpected

-Dspring.profiles.active=local,dev //will use logback-dev.xml
-Dspring.profiles.active=dev,local //will use logback-local.xml

Solution 3

Instead of adding separate logback xmls for each profile or having the IF condition , I would suggest the following (If you have less difference in the xmls') for easy conditional processing :

<springProfile name="dev">
<logger name="org.sample" level="DEBUG" />
</springProfile>
<springProfile name="prod">
<logger name="org.sample" level="TRACE" />
</springProfile>

Solution 4

Conditional processing with logback will be a solution without many logback files. Here is a link and a sample logback configuration with spring profiles.

<configuration>

    <property name="LOG_LEVEL" value="INFO"/>

        <if condition='"product".equals("${spring.profiles.active}")'>
           <then>
                <property name="LOG_LEVEL" value="INFO"/>
           </then>
           <else>
                <property name="LOG_LEVEL" value="ERROR"/>
           </else>
        </if>

         .
         .
         appender, logger tags etc.
         .
         .

         <root level="${LOG_LEVEL}">
             <appender-ref ref="STDOUT"/>
         </root>

</configuration>

Also, you might have to add this to your pom.xml

<dependency>
    <groupId>org.codehaus.janino</groupId>
    <artifactId>janino</artifactId>
    <version>3.0.6</version>
</dependency>

Solution 5

Spring has support of next tag <springProperty/> inside Logback XML file, this tag described here . It means that you can easily add variable from Spring property file, even this variable value resolves from environment/system variable by Spring.

Share:
116,159
LG_
Author by

LG_

Updated on July 08, 2022

Comments

  • LG_
    LG_ almost 2 years

    I am implementing logging in a spring boot project with logback library. I want to load different logging configuration files according to my spring profiles (property 'spring.pofiles.active'). I have 3 files : logback-dev.xml, logback-inte.xml and logback-prod.xml. I am using spring boot version 1.2.2.RELEASE.

    As you can read in spring boot documentation:

    The various logging systems can be activated by including the appropriate libraries on the classpath, and further customized by providing a suitable configuration file in the root of the classpath, or in a location specified by the Spring Environment property logging.config. (Note however that since logging is initialized before the ApplicationContext is created, it isn’t possible to control logging from @PropertySources in Spring @Configuration files. System properties and the conventional Spring Boot external configuration files work just fine.)

    So I tried to set 'logging.config' property in my application.properties file:

    logging.config=classpath:/logback-${spring.profiles.active}.xml
    

    But when i start my application, my logback-{profile}.xml is not loaded...

    I think logging is a common problem that all projects using spring boot have encountered. Am I on the right track with the above approach? I have other solutions that work, but I find them not as elegant (conditional parsing with Janino in logback.xml file or command line property).

  • mjj1409
    mjj1409 about 9 years
    This approach seems good for your use case, however, we may have more than one profile defined in the environment which would cause the include to break. i.e. spring.profiles.active=profile1,profile2. <include resource="logback-profile1,profile2.xml"/>
  • LG_
    LG_ about 9 years
    Yes that's true. In this case i would use janino library.
  • Marsellus Wallace
    Marsellus Wallace almost 9 years
    any updates on an elegant solution for when you have multiple active profiles at the same time?
  • 1in9ui5t
    1in9ui5t over 8 years
    Checkout the springProfile extension coming in Spring Boot 1.3.0
  • amjad
    amjad about 8 years
    you can also define profile in application.properties with default dev as spring.profiles.active=${dev:dev}. If you don't pass -Dspring.profiles.active default will be dev
  • Wim Deblauwe
    Wim Deblauwe about 7 years
  • Amber Kulkarni
    Amber Kulkarni over 6 years
    we still have to specify spring.profiles.active=mode right ?
  • plmaheu
    plmaheu over 3 years
    In case people still stumble on this issue: the actual cause is Tomcat overriding config.location. There's a fix for it in Tomcat 10.