Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
361 additions
and
121 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
.../java/org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright 2012-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.logging; | ||
|
||
import java.util.function.Supplier; | ||
|
||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport; | ||
import org.springframework.boot.logging.LogLevel; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Logs the {@link ConditionEvaluationReport}. | ||
* | ||
* @author Greg Turnquist | ||
* @author Dave Syer | ||
* @author Phillip Webb | ||
* @author Andy Wilkinson | ||
* @author Madhura Bhave | ||
*/ | ||
class ConditionEvaluationReportLogger { | ||
|
||
private final Log logger = LogFactory.getLog(getClass()); | ||
|
||
private final Supplier<ConditionEvaluationReport> reportSupplier; | ||
|
||
private final LogLevel logLevel; | ||
|
||
ConditionEvaluationReportLogger(LogLevel logLevel, Supplier<ConditionEvaluationReport> reportSupplier) { | ||
Assert.isTrue(isInfoOrDebug(logLevel), "LogLevel must be INFO or DEBUG"); | ||
this.logLevel = logLevel; | ||
this.reportSupplier = reportSupplier; | ||
} | ||
|
||
private boolean isInfoOrDebug(LogLevel logLevelForReport) { | ||
return LogLevel.INFO.equals(logLevelForReport) || LogLevel.DEBUG.equals(logLevelForReport); | ||
} | ||
|
||
void logReport(boolean isCrashReport) { | ||
ConditionEvaluationReport report = this.reportSupplier.get(); | ||
if (report == null) { | ||
this.logger.info("Unable to provide the condition evaluation report"); | ||
return; | ||
} | ||
if (!report.getConditionAndOutcomesBySource().isEmpty()) { | ||
if (this.logLevel.equals(LogLevel.INFO)) { | ||
if (this.logger.isInfoEnabled()) { | ||
this.logger.info(new ConditionEvaluationReportMessage(report)); | ||
} | ||
else if (isCrashReport) { | ||
logMessage("info"); | ||
} | ||
} | ||
else { | ||
if (this.logger.isDebugEnabled()) { | ||
this.logger.debug(new ConditionEvaluationReportMessage(report)); | ||
} | ||
else if (isCrashReport) { | ||
logMessage("debug"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void logMessage(String logLevel) { | ||
this.logger.info(String.format("%n%nError starting ApplicationContext. To display the " | ||
+ "condition evaluation report re-run your application with '" + logLevel + "' enabled.")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggingProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2012-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.logging; | ||
|
||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; | ||
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor; | ||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; | ||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport; | ||
import org.springframework.boot.logging.LogLevel; | ||
|
||
/** | ||
* {@link BeanFactoryInitializationAotProcessor} that logs the | ||
* {@link ConditionEvaluationReport} during ahead-of-time processing. | ||
* | ||
* @author Andy Wilkinson | ||
*/ | ||
class ConditionEvaluationReportLoggingProcessor implements BeanFactoryInitializationAotProcessor { | ||
|
||
@Override | ||
public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableListableBeanFactory beanFactory) { | ||
logConditionEvaluationReport(beanFactory); | ||
return null; | ||
} | ||
|
||
private void logConditionEvaluationReport(ConfigurableListableBeanFactory beanFactory) { | ||
new ConditionEvaluationReportLogger(LogLevel.DEBUG, () -> ConditionEvaluationReport.get(beanFactory)) | ||
.logReport(false); | ||
} | ||
|
||
} |
3 changes: 3 additions & 0 deletions
3
...g-boot-project/spring-boot-autoconfigure/src/main/resources/META-INF/spring/aot.factories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
org.springframework.aot.hint.RuntimeHintsRegistrar=\ | ||
org.springframework.boot.autoconfigure.template.TemplateRuntimeHints | ||
|
||
org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor=\ | ||
org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingProcessor |
124 changes: 124 additions & 0 deletions
124
.../org/springframework/boot/autoconfigure/logging/ConditionEvaluationReportLoggerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* | ||
* Copyright 2012-2022 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.logging; | ||
|
||
import java.util.Arrays; | ||
|
||
import ch.qos.logback.classic.Level; | ||
import ch.qos.logback.classic.Logger; | ||
import ch.qos.logback.classic.LoggerContext; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import org.springframework.boot.autoconfigure.condition.ConditionEvaluationReport; | ||
import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListenerTests.Config; | ||
import org.springframework.boot.logging.LogLevel; | ||
import org.springframework.boot.test.system.CapturedOutput; | ||
import org.springframework.boot.test.system.OutputCaptureExtension; | ||
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
|
||
/** | ||
* Tests for {@link ConditionEvaluationReportLogger}. | ||
* | ||
* @author Andy Wilkinson | ||
*/ | ||
@ExtendWith(OutputCaptureExtension.class) | ||
class ConditionEvaluationReportLoggerTests { | ||
|
||
@Test | ||
void noErrorIfNotInitialized(CapturedOutput output) { | ||
new ConditionEvaluationReportLogger(LogLevel.INFO, () -> null).logReport(true); | ||
assertThat(output).contains("Unable to provide the condition evaluation report"); | ||
} | ||
|
||
@Test | ||
void supportsOnlyInfoAndDebugLogLevels() { | ||
assertThatIllegalArgumentException() | ||
.isThrownBy(() -> new ConditionEvaluationReportLogger(LogLevel.TRACE, () -> null)) | ||
.withMessageContaining("LogLevel must be INFO or DEBUG"); | ||
} | ||
|
||
@Test | ||
void loggerWithInfoLevelShouldLogAtInfo(CapturedOutput output) { | ||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { | ||
ConditionEvaluationReportLogger logger = new ConditionEvaluationReportLogger(LogLevel.INFO, | ||
() -> ConditionEvaluationReport.get(context.getBeanFactory())); | ||
context.register(Config.class); | ||
context.refresh(); | ||
logger.logReport(false); | ||
assertThat(output).contains("CONDITIONS EVALUATION REPORT"); | ||
} | ||
} | ||
|
||
@Test | ||
void loggerWithDebugLevelShouldLogAtDebug(CapturedOutput output) { | ||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { | ||
ConditionEvaluationReportLogger logger = new ConditionEvaluationReportLogger(LogLevel.DEBUG, | ||
() -> ConditionEvaluationReport.get(context.getBeanFactory())); | ||
context.register(Config.class); | ||
context.refresh(); | ||
logger.logReport(false); | ||
assertThat(output).doesNotContain("CONDITIONS EVALUATION REPORT"); | ||
withDebugLogging(() -> logger.logReport(false)); | ||
assertThat(output).contains("CONDITIONS EVALUATION REPORT"); | ||
} | ||
} | ||
|
||
@Test | ||
void logsInfoOnErrorIfDebugDisabled(CapturedOutput output) { | ||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { | ||
ConditionEvaluationReportLogger logger = new ConditionEvaluationReportLogger(LogLevel.DEBUG, | ||
() -> ConditionEvaluationReport.get(context.getBeanFactory())); | ||
context.register(Config.class); | ||
context.refresh(); | ||
logger.logReport(true); | ||
assertThat(output).contains("Error starting ApplicationContext. To display the condition " | ||
+ "evaluation report re-run your application with 'debug' enabled."); | ||
} | ||
} | ||
|
||
@Test | ||
void logsOutput(CapturedOutput output) { | ||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { | ||
ConditionEvaluationReportLogger logger = new ConditionEvaluationReportLogger(LogLevel.DEBUG, | ||
() -> ConditionEvaluationReport.get(context.getBeanFactory())); | ||
context.register(Config.class); | ||
ConditionEvaluationReport.get(context.getBeanFactory()).recordExclusions(Arrays.asList("com.foo.Bar")); | ||
context.refresh(); | ||
withDebugLogging(() -> logger.logReport(false)); | ||
assertThat(output).contains("not a servlet web application (OnWebApplicationCondition)"); | ||
} | ||
} | ||
|
||
private void withDebugLogging(Runnable runnable) { | ||
Logger logger = ((LoggerContext) LoggerFactory.getILoggerFactory()) | ||
.getLogger(ConditionEvaluationReportLogger.class); | ||
Level currentLevel = logger.getLevel(); | ||
logger.setLevel(Level.DEBUG); | ||
try { | ||
runnable.run(); | ||
} | ||
finally { | ||
logger.setLevel(currentLevel); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.