Simplify printing ConditionEvaluationReport when using context runner
Closes gh-13119
This commit is contained in:
@@ -31,6 +31,7 @@ import org.springframework.context.event.GenericApplicationListener;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* {@link ApplicationContextInitializer} that writes the {@link ConditionEvaluationReport}
|
||||
@@ -45,6 +46,7 @@ import org.springframework.core.ResolvableType;
|
||||
* @author Dave Syer
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
public class ConditionEvaluationReportLoggingListener
|
||||
implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
@@ -55,6 +57,26 @@ public class ConditionEvaluationReportLoggingListener
|
||||
|
||||
private ConditionEvaluationReport report;
|
||||
|
||||
private final LogLevel logLevelForReport;
|
||||
|
||||
public ConditionEvaluationReportLoggingListener() {
|
||||
this(LogLevel.DEBUG);
|
||||
}
|
||||
|
||||
public ConditionEvaluationReportLoggingListener(LogLevel logLevelForReport) {
|
||||
Assert.isTrue(isInfoOrDebug(logLevelForReport), "LogLevel must be INFO or DEBUG");
|
||||
this.logLevelForReport = logLevelForReport;
|
||||
}
|
||||
|
||||
private boolean isInfoOrDebug(LogLevel logLevelForReport) {
|
||||
return LogLevel.INFO.equals(logLevelForReport)
|
||||
|| LogLevel.DEBUG.equals(logLevelForReport);
|
||||
}
|
||||
|
||||
public LogLevel getLogLevelForReport() {
|
||||
return this.logLevelForReport;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void initialize(ConfigurableApplicationContext applicationContext) {
|
||||
this.applicationContext = applicationContext;
|
||||
@@ -97,19 +119,33 @@ public class ConditionEvaluationReportLoggingListener
|
||||
.get(this.applicationContext.getBeanFactory());
|
||||
}
|
||||
if (!this.report.getConditionAndOutcomesBySource().isEmpty()) {
|
||||
if (isCrashReport && this.logger.isInfoEnabled()
|
||||
&& !this.logger.isDebugEnabled()) {
|
||||
this.logger.info(String
|
||||
.format("%n%nError starting ApplicationContext. To display the "
|
||||
+ "conditions report re-run your application with "
|
||||
+ "'debug' enabled."));
|
||||
if (this.getLogLevelForReport().equals(LogLevel.INFO)) {
|
||||
if (this.logger.isInfoEnabled()) {
|
||||
this.logger.info(new ConditionEvaluationReportMessage(this.report));
|
||||
}
|
||||
else if (isCrashReport) {
|
||||
logMessage("info");
|
||||
}
|
||||
}
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug(new ConditionEvaluationReportMessage(this.report));
|
||||
else {
|
||||
if (isCrashReport && this.logger.isInfoEnabled()
|
||||
&& !this.logger.isDebugEnabled()) {
|
||||
logMessage("debug");
|
||||
}
|
||||
if (this.logger.isDebugEnabled()) {
|
||||
this.logger.debug(new ConditionEvaluationReportMessage(this.report));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void logMessage(String logLevel) {
|
||||
this.logger.info(
|
||||
String.format("%n%nError starting ApplicationContext. To display the "
|
||||
+ "conditions report re-run your application with '" + logLevel
|
||||
+ "' enabled."));
|
||||
}
|
||||
|
||||
private class ConditionEvaluationReportListener
|
||||
implements GenericApplicationListener {
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoCon
|
||||
import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.context.event.ApplicationFailedEvent;
|
||||
import org.springframework.boot.logging.LogLevel;
|
||||
import org.springframework.boot.testsupport.rule.OutputCapture;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -41,6 +42,7 @@ import org.springframework.mock.web.MockServletContext;
|
||||
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
/**
|
||||
@@ -48,6 +50,7 @@ import static org.junit.Assert.fail;
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @author Andy Wilkinson
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
public class ConditionEvaluationReportLoggingListenerTests {
|
||||
|
||||
@@ -137,6 +140,26 @@ public class ConditionEvaluationReportLoggingListenerTests {
|
||||
assertThat(context.getBean(ConditionEvaluationReport.class)).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listenerWithInfoLevelShouldLogAtInfo() {
|
||||
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||
ConditionEvaluationReportLoggingListener initializer = new ConditionEvaluationReportLoggingListener(
|
||||
LogLevel.INFO);
|
||||
initializer.initialize(context);
|
||||
context.register(Config.class);
|
||||
context.refresh();
|
||||
initializer.onApplicationEvent(new ContextRefreshedEvent(context));
|
||||
assertThat(this.outputCapture.toString())
|
||||
.contains("CONDITIONS EVALUATION REPORT");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listenerSupportsOnlyInfoAndDebug() {
|
||||
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(
|
||||
() -> new ConditionEvaluationReportLoggingListener(LogLevel.TRACE))
|
||||
.withMessage("LogLevel must be INFO or DEBUG");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noErrorIfNotInitialized() {
|
||||
this.initializer
|
||||
|
||||
Reference in New Issue
Block a user