Protect against malformed logging.config

Ensure that any user specified logging.config exists. Required for
CI build as bamboo sets a "LOGGING_CONFIG" environment variable.
This commit is contained in:
Phillip Webb 2013-07-08 19:30:34 -07:00
parent 2630aa732f
commit dbd29f36ec
2 changed files with 33 additions and 1 deletions

View File

@ -20,6 +20,8 @@ import java.lang.management.ManagementFactory;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.bootstrap.logging.JavaLoggerConfigurer;
import org.springframework.bootstrap.logging.LogbackConfigurer;
import org.springframework.context.ApplicationContext;
@ -31,6 +33,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils;
import org.springframework.util.Log4jConfigurer;
import org.springframework.util.ResourceUtils;
/**
* An {@link ApplicationContextInitializer} that configures a logging framework depending
@ -157,6 +160,9 @@ public class LoggingApplicationContextInitializer implements
}
};
private final Log logger = LogFactory
.getLog(LoggingApplicationContextInitializer.class);
private final String className;
private final String[] paths;
@ -186,7 +192,16 @@ public class LoggingApplicationContextInitializer implements
// User specified config
if (environment.containsProperty("logging.config")) {
return environment.getProperty("logging.config");
String value = environment.getProperty("logging.config");
try {
ResourceUtils.getURL(value).openStream().close();
return value;
}
catch (Exception ex) {
// Swallow exception and continue
}
this.logger.warn("Logging environment value '" + value
+ "' cannot be opened and will be ignored");
}
// Common patterns

View File

@ -104,6 +104,23 @@ public class LoggingApplicationContextInitializerTests {
assertTrue("Wrong output:\n" + output, output.startsWith("/tmp/spring.log"));
}
@Test
public void testOverrideConfigDoesNotExist() throws Exception {
GenericApplicationContext context = new GenericApplicationContext();
context.getEnvironment().getPropertySources()
.addFirst(new PropertySource<String>("manual") {
@Override
public Object getProperty(String name) {
if ("logging.config".equals(name)) {
return "doesnotexist.xml";
}
return null;
}
});
this.initializer.initialize(context);
// Should not throw
}
@Test
public void testAddLogFileProperty() {
GenericApplicationContext context = new GenericApplicationContext();