Improve exception message to hint that you might need a '-spring' file

See gh-42405
This commit is contained in:
Dmytro Nosan
2024-09-20 21:58:24 +03:00
committed by Phillip Webb
parent 9b72e1f506
commit 86ce051bb5
2 changed files with 7 additions and 2 deletions

View File

@@ -43,7 +43,10 @@ class SpringEnvironmentLookup implements LoggerContextAware, StrLookup {
@Override
public String lookup(String key) {
Assert.state(this.environment != null, "Unable to obtain Spring Environment from LoggerContext");
Assert.state(this.environment != null,
"Unable to obtain Spring Environment from LoggerContext. "
+ "This can happen if your log4j2 configuration filename does not end with '-spring' "
+ "(for example using 'log4j2.xml' instead of 'log4j2-spring.xml')");
return this.environment.getProperty(key);
}

View File

@@ -69,7 +69,9 @@ class SpringEnvironmentLookupTests {
this.loggerContext.removeObject(Log4J2LoggingSystem.ENVIRONMENT_KEY);
Interpolator lookup = createLookup(this.loggerContext);
assertThatIllegalStateException().isThrownBy(() -> lookup.lookup("spring:test"))
.withMessage("Unable to obtain Spring Environment from LoggerContext");
.withMessage("Unable to obtain Spring Environment from LoggerContext. "
+ "This can happen if your log4j2 configuration filename does not end with '-spring' "
+ "(for example using 'log4j2.xml' instead of 'log4j2-spring.xml')");
}
private Interpolator createLookup(LoggerContext context) {