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 95d0f2e1028..7eb7d3fa49e 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 @@ -206,10 +206,11 @@ public class LoggingApplicationListener implements GenericApplicationListener { system.initialize(logConfig, logFile); } catch (Exception ex) { - this.logger.warn("Logging environment value '" + logConfig - + "' cannot be opened and will be ignored " - + "(using default location instead)"); - system.initialize(null, logFile); + System.err + .println("Logging system failed to initialize using configuration from '" + + logConfig + "'"); + ex.printStackTrace(System.err); + throw new IllegalStateException(ex); } } else { diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java b/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java index 4756be24860..6d8dbaa4a95 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java @@ -21,6 +21,7 @@ import java.security.CodeSource; import java.security.ProtectionDomain; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import org.slf4j.ILoggerFactory; @@ -40,6 +41,7 @@ import ch.qos.logback.classic.LoggerContext; import ch.qos.logback.classic.turbo.TurboFilter; import ch.qos.logback.classic.util.ContextInitializer; import ch.qos.logback.core.spi.FilterReply; +import ch.qos.logback.core.status.Status; /** * {@link LoggingSystem} for logback. @@ -122,6 +124,22 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem { throw new IllegalStateException("Could not initialize Logback logging from " + location, ex); } + List statuses = context.getStatusManager().getCopyOfStatusList(); + if (containsError(statuses)) { + for (Status status : statuses) { + System.err.println(status); + } + throw new IllegalStateException("Logback configuration error detected"); + } + } + + private boolean containsError(List statuses) { + for (Status status : statuses) { + if (status.getLevel() == Status.ERROR) { + return true; + } + } + return false; } @Override 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 d2d1548b13b..98033bec439 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 @@ -29,6 +29,7 @@ import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.junit.rules.TemporaryFolder; import org.slf4j.bridge.SLF4JBridgeHandler; import org.springframework.boot.SpringApplication; @@ -56,6 +57,9 @@ public class LoggingApplicationListenerTests { private static final String[] NO_ARGS = {}; + @Rule + public ExpectedException thrown = ExpectedException.none(); + @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @@ -129,14 +133,23 @@ public class LoggingApplicationListenerTests { public void overrideConfigDoesNotExist() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "logging.config: doesnotexist.xml"); + this.thrown.expect(IllegalStateException.class); + this.outputCapture + .expect(containsString("Logging system failed to initialize using configuration from 'doesnotexist.xml'")); + this.initializer.initialize(this.context.getEnvironment(), + this.context.getClassLoader()); + } + + @Test + public void overrideConfigBroken() throws Exception { + EnvironmentTestUtils.addEnvironment(this.context, + "logging.config: classpath:logback-broken.xml"); + this.thrown.expect(IllegalStateException.class); + this.outputCapture + .expect(containsString("Logging system failed to initialize using configuration from 'classpath:logback-broken.xml'")); + this.outputCapture.expect(containsString("ConsolAppender")); this.initializer.initialize(this.context.getEnvironment(), this.context.getClassLoader()); - // Should not throw - 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 diff --git a/spring-boot/src/test/resources/logback-broken.xml b/spring-boot/src/test/resources/logback-broken.xml new file mode 100644 index 00000000000..6411a145d45 --- /dev/null +++ b/spring-boot/src/test/resources/logback-broken.xml @@ -0,0 +1,11 @@ + + + + + ${LOG_FILE} [%t] ${PID:-????} %c{1}: %m%n BOOTBOOT + + + + + +