Spring-Boot Logging configuration when deployed as

2019-01-18 23:48发布

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>

1条回答
smile是对你的礼貌
2楼-- · 2019-01-19 00:30

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.

查看更多
登录 后发表回答