Merge branch '3.0.x' into 3.1.x

Closes gh-36856
3.1.x
Andy Wilkinson 1 year ago
commit 0c9e6268d5

@ -248,20 +248,29 @@ public class LogbackLoggingSystem extends AbstractLoggingSystem implements BeanF
} }
private void reportConfigurationErrorsIfNecessary(LoggerContext loggerContext) { private void reportConfigurationErrorsIfNecessary(LoggerContext loggerContext) {
List<Status> statuses = loggerContext.getStatusManager().getCopyOfStatusList();
StringBuilder errors = new StringBuilder(); StringBuilder errors = new StringBuilder();
for (Status status : statuses) { List<Throwable> suppressedExceptions = new ArrayList<>();
for (Status status : loggerContext.getStatusManager().getCopyOfStatusList()) {
if (status.getLevel() == Status.ERROR) { if (status.getLevel() == Status.ERROR) {
errors.append((errors.length() > 0) ? String.format("%n") : ""); errors.append((errors.length() > 0) ? String.format("%n") : "");
errors.append(status.toString()); errors.append(status.toString());
if (status.getThrowable() != null) {
suppressedExceptions.add(status.getThrowable());
}
} }
} }
if (errors.length() > 0) { if (errors.length() == 0) {
throw new IllegalStateException(String.format("Logback configuration error detected: %n%s", errors)); if (!StatusUtil.contextHasStatusListener(loggerContext)) {
StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext);
}
return;
} }
if (!StatusUtil.contextHasStatusListener(loggerContext)) { IllegalStateException ex = new IllegalStateException(
StatusPrinter.printInCaseOfErrorsOrWarnings(loggerContext); String.format("Logback configuration error detected: %n%s", errors));
for (Throwable suppressedException : suppressedExceptions) {
ex.addSuppressed(suppressedException);
} }
throw ex;
} }
private void configureByResourceUrl(LoggingInitializationContext initializationContext, LoggerContext loggerContext, private void configureByResourceUrl(LoggingInitializationContext initializationContext, LoggerContext loggerContext,

@ -38,6 +38,7 @@ import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder; import ch.qos.logback.core.encoder.LayoutWrappingEncoder;
import ch.qos.logback.core.rolling.RollingFileAppender; import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy; import ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy;
import ch.qos.logback.core.util.DynamicClassLoadingException;
import ch.qos.logback.core.util.StatusPrinter; import ch.qos.logback.core.util.StatusPrinter;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
@ -676,6 +677,16 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests {
assertThat(output).contains("<springProfile> elements cannot be nested within an"); assertThat(output).contains("<springProfile> elements cannot be nested within an");
} }
@Test
void whenConfigurationErrorIsDetectedUnderlyingCausesAreIncludedAsSuppressedExceptions() {
this.loggingSystem.beforeInitialize();
assertThatIllegalStateException()
.isThrownBy(() -> initialize(this.initializationContext, "classpath:logback-broken.xml",
getLogFile(tmpDir() + "/tmp.log", null)))
.satisfies((ex) -> assertThat(ex.getSuppressed())
.hasAtLeastOneElementOfType(DynamicClassLoadingException.class));
}
private void initialize(LoggingInitializationContext context, String configLocation, LogFile logFile) { private void initialize(LoggingInitializationContext context, String configLocation, LogFile logFile) {
this.loggingSystem.getSystemProperties((ConfigurableEnvironment) context.getEnvironment()).apply(logFile); this.loggingSystem.getSystemProperties((ConfigurableEnvironment) context.getEnvironment()).apply(logFile);
this.loggingSystem.initialize(context, configLocation, logFile); this.loggingSystem.initialize(context, configLocation, logFile);

Loading…
Cancel
Save