Skip to content

Commit d4df6f7

Browse files
committed
Merge pull request #42405 from nosan
* pr/42405: Polish 'Improve exception message to hint that you might need a '-spring' file' Improve exception message to hint that you might need a '-spring' file Closes gh-42405
2 parents 9b72e1f + 9b49e93 commit d4df6f7

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@
3030
*
3131
* @author Ralph Goers
3232
* @author Phillip Webb
33+
* @author Dmytro Nosan
3334
*/
3435
@Plugin(name = "spring", category = StrLookup.CATEGORY)
3536
class SpringEnvironmentLookup implements LoggerContextAware, StrLookup {
@@ -43,7 +44,10 @@ public String lookup(LogEvent event, String key) {
4344

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

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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)