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
This commit is contained in:
Phillip Webb
2024-09-22 20:35:45 -07:00
2 changed files with 10 additions and 4 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2022 the original author or authors.
* Copyright 2012-2024 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.
@@ -30,6 +30,7 @@ import org.springframework.util.Assert;
*
* @author Ralph Goers
* @author Phillip Webb
* @author Dmytro Nosan
*/
@Plugin(name = "spring", category = StrLookup.CATEGORY)
class SpringEnvironmentLookup implements LoggerContextAware, StrLookup {
@@ -43,7 +44,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

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 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.
@@ -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) {