Skip to content

Commit f82f4d6

Browse files
committed
Merge branch '2.7.x' into 3.0.x
Closes gh-36855
2 parents 4ec1a05 + 694ff4f commit f82f4d6

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
@@ -248,20 +248,29 @@ protected void loadConfiguration(LoggingInitializationContext initializationCont
248248
}
249249

250250
private void reportConfigurationErrorsIfNecessary(LoggerContext loggerContext) {
251-
List<Status> statuses = loggerContext.getStatusManager().getCopyOfStatusList();
252251
StringBuilder errors = new StringBuilder();
253-
for (Status status : statuses) {
252+
List<Throwable> suppressedExceptions = new ArrayList<>();
253+
for (Status status : loggerContext.getStatusManager().getCopyOfStatusList()) {
254254
if (status.getLevel() == Status.ERROR) {
255255
errors.append((errors.length() > 0) ? String.format("%n") : "");
256256
errors.append(status.toString());
257+
if (status.getThrowable() != null) {
258+
suppressedExceptions.add(status.getThrowable());
259+
}
257260
}
258261
}
259-
if (errors.length() > 0) {
260-
throw new IllegalStateException(String.format("Logback configuration error detected: %n%s", errors));
262+
if (errors.length() == 0) {
263+
if (!StatusUtil.contextHasStatusListener(loggerContext)) {
264+
StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
265+
}
266+
return;
261267
}
262-
if (!StatusUtil.contextHasStatusListener(loggerContext)) {
263-
StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
268+
IllegalStateException ex = new IllegalStateException(
269+
String.format("Logback configuration error detected: %n%s", errors));
270+
for (Throwable suppressedException : suppressedExceptions) {
271+
ex.addSuppressed(suppressedException);
264272
}
273+
throw ex;
265274
}
266275

267276
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;
@@ -676,6 +677,16 @@ void springProfileIfNestedWithinSecondPhaseElementSanityChecker(CapturedOutput o
676677
assertThat(output).contains("<springProfile> elements cannot be nested within an");
677678
}
678679

680+
@Test
681+
void whenConfigurationErrorIsDetectedUnderlyingCausesAreIncludedAsSuppressedExceptions() {
682+
this.loggingSystem.beforeInitialize();
683+
assertThatIllegalStateException()
684+
.isThrownBy(() -> initialize(this.initializationContext, "classpath:logback-broken.xml",
685+
getLogFile(tmpDir() + "/tmp.log", null)))
686+
.satisfies((ex) -> assertThat(ex.getSuppressed())
687+
.hasAtLeastOneElementOfType(DynamicClassLoadingException.class));
688+
}
689+
679690
private void initialize(LoggingInitializationContext context, String configLocation, LogFile logFile) {
680691
this.loggingSystem.getSystemProperties((ConfigurableEnvironment) context.getEnvironment()).apply(logFile);
681692
this.loggingSystem.initialize(context, configLocation, logFile);

0 commit comments

Comments
 (0)