From a19f1a733e71543b903c7555b10ba6ed66ecef39 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 8 Nov 2022 12:54:46 +0000 Subject: [PATCH] Correct crash handling in condition report logging listener Closes gh-33027 --- ...ditionEvaluationReportLoggingListener.java | 2 +- ...nEvaluationReportLoggingListenerTests.java | 22 ++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListener.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListener.java index 3dcbd5dafd..31bcac1d4f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListener.java +++ b/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 && applicationFailedEvent.getApplicationContext() == this.context) { - this.logger.logReport(false); + this.logger.logReport(true); } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java index 7bc8fb2db7..9629cc94b1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingListenerTests.java +++ b/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"); } + @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(); }