Browse Source

Correct crash handling in condition report logging listener

Closes gh-33027
pull/33080/head
Andy Wilkinson 3 years ago
parent
commit
a19f1a733e
  1. 2
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListener.java
  2. 22
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java

2
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListener.java

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

22
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java

@ -73,6 +73,18 @@ class ConditionEvaluationReportLoggingListenerTests {
assertThat(output).contains("CONDITIONS EVALUATION REPORT"); 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 @Test
void canBeUsedInApplicationContext() { void canBeUsedInApplicationContext() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
@ -93,10 +105,18 @@ class ConditionEvaluationReportLoggingListenerTests {
} }
private void withDebugLogging(Runnable runnable) { 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()) Logger logger = ((LoggerContext) LoggerFactory.getILoggerFactory())
.getLogger(ConditionEvaluationReportLogger.class); .getLogger(ConditionEvaluationReportLogger.class);
Level currentLevel = logger.getLevel(); Level currentLevel = logger.getLevel();
logger.setLevel(Level.DEBUG); logger.setLevel(logLevel);
try { try {
runnable.run(); runnable.run();
} }

Loading…
Cancel
Save