Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
963 views
in Technique[技术] by (71.8m points)

tomcat - Spring-Boot Logging configuration when deployed as .war

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...