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.
pull/7/head
Phillip Webb 12 years ago
parent 2630aa732f
commit dbd29f36ec

@ -20,6 +20,8 @@ import java.lang.management.ManagementFactory;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; 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.JavaLoggerConfigurer;
import org.springframework.bootstrap.logging.LogbackConfigurer; import org.springframework.bootstrap.logging.LogbackConfigurer;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -31,6 +33,7 @@ import org.springframework.core.env.Environment;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.Log4jConfigurer; import org.springframework.util.Log4jConfigurer;
import org.springframework.util.ResourceUtils;
/** /**
* An {@link ApplicationContextInitializer} that configures a logging framework depending * 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 className;
private final String[] paths; private final String[] paths;
@ -186,7 +192,16 @@ public class LoggingApplicationContextInitializer implements
// User specified config // User specified config
if (environment.containsProperty("logging.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 // Common patterns

@ -104,6 +104,23 @@ public class LoggingApplicationContextInitializerTests {
assertTrue("Wrong output:\n" + output, output.startsWith("/tmp/spring.log")); 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 @Test
public void testAddLogFileProperty() { public void testAddLogFileProperty() {
GenericApplicationContext context = new GenericApplicationContext(); GenericApplicationContext context = new GenericApplicationContext();

Loading…
Cancel
Save