Ignore Azure’s default LOGGING_CONFIG env var when initialising logging
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
This commit is contained in:
parent
8225a8a7d5
commit
66d4a2a49e
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Reference in New Issue