From 66d4a2a49e2b2a4f3730426a91111085f282ab0f Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 8 Jul 2015 21:58:54 +0100 Subject: [PATCH] =?UTF-8?q?Ignore=20Azure=E2=80=99s=20default=20LOGGING=5F?= =?UTF-8?q?CONFIG=20env=20var=20when=20initialising=20logging?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When using Tomcat, Azure automatically configures an environment variable called LOGGING_CONFIG that configures the java.util.logging.config.file system property. LoggingApplicationListener finds this configuration via the Spring environment (it looks for logging.config) and attempts to use it as the name of the logging configuration file. Since c3d93f7 this failure causes the app to fail to start, rather than the previous behaviour of silently falling back to the default configuration. This commit updates LoggingApplicationListener to only consider configuration that is a non-empty string and that does not start with -Djava.util.logging.config.file=, which is the beginning of the default configuration on Azure, and is very unlikely to be part of the name of a logging configuration file. Closes gh-3366 --- .../logging/LoggingApplicationListener.java | 18 +++++++++++++----- .../LoggingApplicationListenerTests.java | 15 +++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java index 25979d15bae..ba80ce09af4 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/LoggingApplicationListener.java @@ -202,7 +202,10 @@ public class LoggingApplicationListener implements GenericApplicationListener { environment); LogFile logFile = LogFile.get(environment); String logConfig = environment.getProperty(CONFIG_PROPERTY); - if (StringUtils.hasLength(logConfig)) { + if (ignoreLogConfig(logConfig)) { + system.initialize(initializationContext, null, logFile); + } + else { try { ResourceUtils.getURL(logConfig).openStream().close(); system.initialize(initializationContext, logConfig, logFile); @@ -211,13 +214,18 @@ public class LoggingApplicationListener implements GenericApplicationListener { // NOTE: We can't use the logger here to report the problem System.err.println("Logging system failed to initialize " + "using configuration from '" + logConfig + "'"); - ex.printStackTrace(System.err); throw new IllegalStateException(ex); } } - else { - system.initialize(initializationContext, null, logFile); - } + } + + private boolean ignoreLogConfig(String logConfig) { + return !StringUtils.hasLength(logConfig) + || isDefaultAzureLoggingConfig(logConfig); + } + + private boolean isDefaultAzureLoggingConfig(String candidate) { + return candidate.startsWith("-Djava.util.logging.config.file="); } private void initializeFinalLoggingLevels(ConfigurableEnvironment environment, diff --git a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java index 4e3163040a3..7f6c9c4cdc8 100644 --- a/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/logging/LoggingApplicationListenerTests.java @@ -141,6 +141,21 @@ public class LoggingApplicationListenerTests { this.context.getClassLoader()); } + @Test + public void azureDefaultLoggingConfigDoesNotCauseAFailure() throws Exception { + EnvironmentTestUtils + .addEnvironment( + this.context, + "logging.config: -Djava.util.logging.config.file=\"d:\\home\\site\\wwwroot\\bin\\apache-tomcat-7.0.52\\conf\\logging.properties\""); + this.initializer.initialize(this.context.getEnvironment(), + this.context.getClassLoader()); + this.logger.info("Hello world"); + String output = this.outputCapture.toString().trim(); + assertTrue("Wrong output:\n" + output, output.contains("Hello world")); + assertFalse("Wrong output:\n" + output, output.contains("???")); + assertFalse(new File(tmpDir() + "/spring.log").exists()); + } + @Test public void overrideConfigBroken() throws Exception { EnvironmentTestUtils.addEnvironment(this.context,