Spring-Boot Logging configuration when deployed as .war

15,922

I've been struggling with what might be the same issue - logging works fine from unit tests and running the application 'main()' method, but in tomcat it ignores my configuration completely.

It turns out that on startup, tomcat sets an environment variable 'LOGGING_CONFIG' to describe the location of its 'logging.properties' file. This can be seen in tomcat/bin/catalina.sh or (.bat).

Spring Boot also reads the environment on startup, and as part of its smart handling of configuration, I believe it converts LOGGING_CONFIG to 'logging.config', which is a property it uses to allow you to override the location of its' logging configuration.

The issue is that it then tries to treat that value as a filename and load it (it isn't actually a filename but a Java system property that describes a file), but it fails, and doesn't initialise its logging properly and continues with whatever Tomcat setup by default.

The solution appears to be to ensure that the environment variable is set first, for example something like (your IDE should have an option for this - in Intellij go to Run -> Edit Configurations -> Environment Variables -> enter the name and value from the following):

set logging.config=classpath:/logback.xml

Spring Boot will use this in preference to LOGGING_CONFIG from Tomcat and will work as expected.

So far however I have been unable to get this working from within a configuration file like application.properties, which means that this is a global setting for all applications deployed to that Tomcat instance.

EDIT: this would be due to the fact that environment variables override local configuration anyway, which in every other case but this would be what you wanted :-(

EDIT 2: I can confirm that as of Spring Boot 1.1.6, this issue has been worked around - it is logged as a warning and it will continue to use the existing (and correct) logging configuration. Sadly you can't disable the warning message itself inside your logback.xml file.

Share:
15,922
elpisu
Author by

elpisu

Updated on June 07, 2022

Comments

  • elpisu
    elpisu almost 2 years

    I have a simple spring-boot application being packaged as .war file capable of being deployed to an external Tomcat container. I have a logback.xml (below) in the classpath (/WEB-INF/classes), however, when deployed to Tomcat the log is not written to the file.

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <include resource="org/springframework/boot/logging/logback/base.xml"/>
        <logger name="org.springframework.web" level="DEBUG"/>
    </configuration>
    
  • Dave Syer
    Dave Syer over 9 years
    Wow, I bet that took some tracking down.
  • Dave Syer
    Dave Syer over 9 years
    Some discussion in Github about what to do about it: github.com/spring-projects/spring-boot/issues/1432.
  • mjj1409
    mjj1409 about 9 years
    I'm experiencing the same issue(tomcats LOGGING_CONFIG is taking precedence)however, I'm still hoping to leverage logging.config setting in application.properties that points location different from the default (i.e. classpath:logback.xml) Any ideas for possible workarounds?
  • powder366
    powder366 about 7 years
    Where do you set this in IntelliJ?
  • Patrick Herrera
    Patrick Herrera about 7 years
    @powder366 I've updated my answer to cover this (from memory)