diff --git a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/initializer/LoggingApplicationContextInitializer.java b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/initializer/LoggingApplicationContextInitializer.java index f40e90c849..fe90687dfc 100644 --- a/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/initializer/LoggingApplicationContextInitializer.java +++ b/spring-bootstrap/src/main/java/org/springframework/bootstrap/context/initializer/LoggingApplicationContextInitializer.java @@ -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 diff --git a/spring-bootstrap/src/test/java/org/springframework/bootstrap/logging/LoggingApplicationContextInitializerTests.java b/spring-bootstrap/src/test/java/org/springframework/bootstrap/logging/LoggingApplicationContextInitializerTests.java index 4f25ff7027..4872038a5f 100644 --- a/spring-bootstrap/src/test/java/org/springframework/bootstrap/logging/LoggingApplicationContextInitializerTests.java +++ b/spring-bootstrap/src/test/java/org/springframework/bootstrap/logging/LoggingApplicationContextInitializerTests.java @@ -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("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();