Skip to content

Commit 86ce051

Browse files
nosanphilwebb
authored andcommitted
Improve exception message to hint that you might need a '-spring' file
See gh-42405
1 parent 9b72e1f commit 86ce051

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookup.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ public String lookup(LogEvent event, String key) {
4343

4444
@Override
4545
public String lookup(String key) {
46-
Assert.state(this.environment != null, "Unable to obtain Spring Environment from LoggerContext");
46+
Assert.state(this.environment != null,
47+
"Unable to obtain Spring Environment from LoggerContext. "
48+
+ "This can happen if your log4j2 configuration filename does not end with '-spring' "
49+
+ "(for example using 'log4j2.xml' instead of 'log4j2-spring.xml')");
4750
return this.environment.getProperty(key);
4851
}
4952

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookupTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ void lookupWhenNoSpringEnvironmentThrowsException() {
6969
this.loggerContext.removeObject(Log4J2LoggingSystem.ENVIRONMENT_KEY);
7070
Interpolator lookup = createLookup(this.loggerContext);
7171
assertThatIllegalStateException().isThrownBy(() -> lookup.lookup("spring:test"))
72-
.withMessage("Unable to obtain Spring Environment from LoggerContext");
72+
.withMessage("Unable to obtain Spring Environment from LoggerContext. "
73+
+ "This can happen if your log4j2 configuration filename does not end with '-spring' "
74+
+ "(for example using 'log4j2.xml' instead of 'log4j2-spring.xml')");
7375
}
7476

7577
private Interpolator createLookup(LoggerContext context) {

0 commit comments

Comments
 (0)