From 86ce051bb5c1e5dfe18380a89977d756b94d016d Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Fri, 20 Sep 2024 21:58:24 +0300 Subject: [PATCH 1/2] Improve exception message to hint that you might need a '-spring' file See gh-42405 --- .../boot/logging/log4j2/SpringEnvironmentLookup.java | 5 ++++- .../boot/logging/log4j2/SpringEnvironmentLookupTests.java | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) 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..1a0b16995e 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 @@ -43,7 +43,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..03027166e7 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 @@ -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) { From 9b49e93f6214ef800fef2d46d0c106967ac559f9 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Sun, 22 Sep 2024 20:33:41 -0700 Subject: [PATCH 2/2] Polish 'Improve exception message to hint that you might need a '-spring' file' See gh-42405 --- .../boot/logging/log4j2/SpringEnvironmentLookup.java | 3 ++- .../boot/logging/log4j2/SpringEnvironmentLookupTests.java | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) 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 1a0b16995e..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 { 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 03027166e7..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.