Skip to content

Commit 694ff4f

Browse files
committed
Include throwables from Logback errors when reporting invalid config
Closes gh-32562
1 parent 45b62b1 commit 694ff4f

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,30 @@ protected void loadConfiguration(LoggingInitializationContext initializationCont
167167
catch (Exception ex) {
168168
throw new IllegalStateException("Could not initialize Logback logging from " + location, ex);
169169
}
170-
List<Status> statuses = loggerContext.getStatusManager().getCopyOfStatusList();
170+
reportConfigurationErrorsIfNecessary(loggerContext);
171+
}
172+
173+
private void reportConfigurationErrorsIfNecessary(LoggerContext loggerContext) {
171174
StringBuilder errors = new StringBuilder();
172-
for (Status status : statuses) {
175+
List<Throwable> suppressedExceptions = new ArrayList<>();
176+
for (Status status : loggerContext.getStatusManager().getCopyOfStatusList()) {
173177
if (status.getLevel() == Status.ERROR) {
174178
errors.append((errors.length() > 0) ? String.format("%n") : "");
175179
errors.append(status.toString());
180+
if (status.getThrowable() != null) {
181+
suppressedExceptions.add(status.getThrowable());
182+
}
176183
}
177184
}
178-
if (errors.length() > 0) {
179-
throw new IllegalStateException(String.format("Logback configuration error detected: %n%s", errors));
185+
if (errors.length() == 0) {
186+
return;
187+
}
188+
IllegalStateException ex = new IllegalStateException(
189+
String.format("Logback configuration error detected: %n%s", errors));
190+
for (Throwable suppressedException : suppressedExceptions) {
191+
ex.addSuppressed(suppressedException);
180192
}
193+
throw ex;
181194
}
182195

183196
private void configureByResourceUrl(LoggingInitializationContext initializationContext, LoggerContext loggerContext,

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

Lines changed: 12 additions & 1 deletion
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 org.junit.jupiter.api.AfterEach;
4243
import org.junit.jupiter.api.BeforeEach;
4344
import org.junit.jupiter.api.Test;
@@ -519,7 +520,7 @@ void initializeShouldApplyLogbackSystemPropertiesToTheContext() {
519520
initialize(this.initializationContext, null, null);
520521
LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();
521522
Map<String, String> properties = loggerContext.getCopyOfPropertyMap();
522-
Set<String> expectedProperties = new HashSet<String>();
523+
Set<String> expectedProperties = new HashSet<>();
523524
ReflectionUtils.doWithFields(LogbackLoggingSystemProperties.class,
524525
(field) -> expectedProperties.add((String) field.get(null)), this::isPublicStaticFinal);
525526
expectedProperties.removeAll(Arrays.asList("LOG_FILE", "LOG_PATH"));
@@ -624,6 +625,16 @@ void customCharset() {
624625
assertThat(encoder.getCharset()).isEqualTo(StandardCharsets.UTF_16);
625626
}
626627

628+
@Test
629+
void whenConfigurationErrorIsDetectedUnderlyingCausesAreIncludedAsSuppressedExceptions() {
630+
this.loggingSystem.beforeInitialize();
631+
assertThatIllegalStateException()
632+
.isThrownBy(() -> initialize(this.initializationContext, "classpath:logback-broken.xml",
633+
getLogFile(tmpDir() + "/tmp.log", null)))
634+
.satisfies((ex) -> assertThat(ex.getSuppressed())
635+
.hasAtLeastOneElementOfType(DynamicClassLoadingException.class));
636+
}
637+
627638
private void initialize(LoggingInitializationContext context, String configLocation, LogFile logFile) {
628639
this.loggingSystem.getSystemProperties((ConfigurableEnvironment) context.getEnvironment()).apply(logFile);
629640
this.loggingSystem.initialize(context, configLocation, logFile);

0 commit comments

Comments
 (0)