Skip to content

Commit bd0652f

Browse files
committed
Merge branch '3.1.x'
Closes gh-36857
2 parents 4f9fcfc + 0c9e626 commit bd0652f

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,29 @@ protected void loadConfiguration(LoggingInitializationContext initializationCont
250250
}
251251

252252
private void reportConfigurationErrorsIfNecessary(LoggerContext loggerContext) {
253-
List<Status> statuses = loggerContext.getStatusManager().getCopyOfStatusList();
254253
StringBuilder errors = new StringBuilder();
255-
for (Status status : statuses) {
254+
List<Throwable> suppressedExceptions = new ArrayList<>();
255+
for (Status status : loggerContext.getStatusManager().getCopyOfStatusList()) {
256256
if (status.getLevel() == Status.ERROR) {
257257
errors.append((errors.length() > 0) ? String.format("%n") : "");
258258
errors.append(status.toString());
259+
if (status.getThrowable() != null) {
260+
suppressedExceptions.add(status.getThrowable());
261+
}
259262
}
260263
}
261-
if (errors.length() > 0) {
262-
throw new IllegalStateException(String.format("Logback configuration error detected: %n%s", errors));
264+
if (errors.length() == 0) {
265+
if (!StatusUtil.contextHasStatusListener(loggerContext)) {
266+
StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
267+
}
268+
return;
263269
}
264-
if (!StatusUtil.contextHasStatusListener(loggerContext)) {
265-
StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
270+
IllegalStateException ex = new IllegalStateException(
271+
String.format("Logback configuration error detected: %n%s", errors));
272+
for (Throwable suppressedException : suppressedExceptions) {
273+
ex.addSuppressed(suppressedException);
266274
}
275+
throw ex;
267276
}
268277

269278
private void configureByResourceUrl(LoggingInitializationContext initializationContext, LoggerContext loggerContext,

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
3939
import ch.qos.logback.core.rolling.RollingFileAppender;
4040
import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
41+
import ch.qos.logback.core.util.DynamicClassLoadingException;
4142
import ch.qos.logback.core.util.StatusPrinter;
4243
import org.junit.jupiter.api.AfterEach;
4344
import org.junit.jupiter.api.BeforeEach;
@@ -771,6 +772,16 @@ void applicationNameLoggingWhenDisabled(CapturedOutput output) {
771772
assertThat(getLineWithText(output, "Hello world")).doesNotContain("myapp");
772773
}
773774

775+
@Test
776+
void whenConfigurationErrorIsDetectedUnderlyingCausesAreIncludedAsSuppressedExceptions() {
777+
this.loggingSystem.beforeInitialize();
778+
assertThatIllegalStateException()
779+
.isThrownBy(() -> initialize(this.initializationContext, "classpath:logback-broken.xml",
780+
getLogFile(tmpDir() + "/tmp.log", null)))
781+
.satisfies((ex) -> assertThat(ex.getSuppressed())
782+
.hasAtLeastOneElementOfType(DynamicClassLoadingException.class));
783+
}
784+
774785
private void initialize(LoggingInitializationContext context, String configLocation, LogFile logFile) {
775786
this.loggingSystem.getSystemProperties((ConfigurableEnvironment) context.getEnvironment()).apply(logFile);
776787
this.loggingSystem.beforeInitialize();

0 commit comments

Comments
 (0)