diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookup.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookup.java index 48989abfc3..83b4ec6489 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookup.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookup.java @@ -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); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookupTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookupTests.java index a8b6d39c06..a45c79cf6a 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookupTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/SpringEnvironmentLookupTests.java @@ -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) {