Correct crash handling in condition report logging listener

Closes gh-33027
pull/33080/head
Andy Wilkinson 2 years ago
parent 9d76f43299
commit a19f1a733e

@ -137,7 +137,7 @@ public class ConditionEvaluationReportLoggingListener
}
else if (event instanceof ApplicationFailedEvent applicationFailedEvent
&& applicationFailedEvent.getApplicationContext() == this.context) {
this.logger.logReport(false);
this.logger.logReport(true);
}
}

@ -73,6 +73,18 @@ class ConditionEvaluationReportLoggingListenerTests {
assertThat(output).contains("CONDITIONS EVALUATION REPORT");
}
@Test
void logsInfoGuidanceToEnableDebugLoggingOnApplicationFailedEvent(CapturedOutput output) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
this.initializer.initialize(context);
context.register(ErrorConfig.class);
assertThatExceptionOfType(Exception.class).isThrownBy(context::refresh)
.satisfies((ex) -> withInfoLogging(() -> context.publishEvent(
new ApplicationFailedEvent(new SpringApplication(), new String[0], context, ex))));
assertThat(output).doesNotContain("CONDITONS EVALUATION REPORT")
.contains("re-run your application with 'debug' enabled");
}
@Test
void canBeUsedInApplicationContext() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@ -93,10 +105,18 @@ class ConditionEvaluationReportLoggingListenerTests {
}
private void withDebugLogging(Runnable runnable) {
withLoggingLevel(Level.DEBUG, runnable);
}
private void withInfoLogging(Runnable runnable) {
withLoggingLevel(Level.INFO, runnable);
}
private void withLoggingLevel(Level logLevel, Runnable runnable) {
Logger logger = ((LoggerContext) LoggerFactory.getILoggerFactory())
.getLogger(ConditionEvaluationReportLogger.class);
Level currentLevel = logger.getLevel();
logger.setLevel(Level.DEBUG);
logger.setLevel(logLevel);
try {
runnable.run();
}

Loading…
Cancel
Save