Skip to content

Commit

Permalink
Improve exception message to hint that you might need a '-spring' file
Browse files Browse the repository at this point in the history
  • Loading branch information
nosan authored and philwebb committed Sep 23, 2024
1 parent 9b72e1f commit 86ce051
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public String lookup(LogEvent event, String key) {

@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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ void lookupWhenNoSpringEnvironmentThrowsException() {
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) {
Expand Down

0 comments on commit 86ce051

Please sign in to comment.