Skip to content

Commit 43e7ccd

Browse files
committed
Detect log4j2-test.* files when using log4J2
Fixes gh-17001
1 parent 98d27db commit 43e7ccd

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ protected String[] getStandardConfigLocations() {
109109

110110
private String[] getCurrentlySupportedConfigLocations() {
111111
List<String> supportedConfigLocations = new ArrayList<>();
112+
addTestFiles(supportedConfigLocations);
112113
supportedConfigLocations.add("log4j2.properties");
113114
if (isClassAvailable("com.fasterxml.jackson.dataformat.yaml.YAMLParser")) {
114115
Collections.addAll(supportedConfigLocations, "log4j2.yaml", "log4j2.yml");
@@ -120,6 +121,17 @@ private String[] getCurrentlySupportedConfigLocations() {
120121
return StringUtils.toStringArray(supportedConfigLocations);
121122
}
122123

124+
private void addTestFiles(List<String> supportedConfigLocations) {
125+
supportedConfigLocations.add("log4j2-test.properties");
126+
if (isClassAvailable("com.fasterxml.jackson.dataformat.yaml.YAMLParser")) {
127+
Collections.addAll(supportedConfigLocations, "log4j2-test.yaml", "log4j2-test.yml");
128+
}
129+
if (isClassAvailable("com.fasterxml.jackson.databind.ObjectMapper")) {
130+
Collections.addAll(supportedConfigLocations, "log4j2-test.json", "log4j2-test.jsn");
131+
}
132+
supportedConfigLocations.add("log4j2-test.xml");
133+
}
134+
123135
protected boolean isClassAvailable(String className) {
124136
return ClassUtils.isPresent(className, getClassLoader());
125137
}

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.springframework.boot.logging.LoggingSystemProperties;
4343
import org.springframework.boot.testsupport.system.CapturedOutput;
4444
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
45+
import org.springframework.util.ClassUtils;
4546
import org.springframework.util.StringUtils;
4647

4748
import static org.assertj.core.api.Assertions.assertThat;
@@ -59,6 +60,7 @@
5960
* @author Phillip Webb
6061
* @author Andy Wilkinson
6162
* @author Ben Hale
63+
* @author Madhura Bhave
6264
*/
6365
@ExtendWith(OutputCaptureExtension.class)
6466
class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@@ -101,7 +103,8 @@ void noFile(CapturedOutput output) {
101103
void withFile(CapturedOutput output) {
102104
this.loggingSystem.beforeInitialize();
103105
this.logger.info("Hidden");
104-
this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir()));
106+
this.loggingSystem.initialize(null, getRelativeClasspathLocation("log4j2-file.xml"),
107+
getLogFile(null, tmpDir()));
105108
this.logger.info("Hello world");
106109
Configuration configuration = this.loggingSystem.getConfiguration();
107110
assertThat(output).contains("Hello world").doesNotContain("Hidden");
@@ -197,39 +200,47 @@ void loggingThatUsesJulIsCaptured(CapturedOutput output) {
197200

198201
@Test
199202
void configLocationsWithNoExtraDependencies() {
200-
assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.properties", "log4j2.xml");
203+
assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2-test.properties",
204+
"log4j2-test.xml", "log4j2.properties", "log4j2.xml");
201205
}
202206

203207
@Test
204208
void configLocationsWithJacksonDatabind() {
205209
this.loggingSystem.availableClasses(ObjectMapper.class.getName());
206-
assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.json", "log4j2.jsn", "log4j2.xml");
210+
assertThat(this.loggingSystem.getStandardConfigLocations()).containsExactly("log4j2-test.properties",
211+
"log4j2-test.json", "log4j2-test.jsn", "log4j2-test.xml", "log4j2.properties", "log4j2.json",
212+
"log4j2.jsn", "log4j2.xml");
207213
}
208214

209215
@Test
210216
void configLocationsWithJacksonDataformatYaml() {
211217
this.loggingSystem.availableClasses("com.fasterxml.jackson.dataformat.yaml.YAMLParser");
212-
assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.yaml", "log4j2.yml", "log4j2.xml");
218+
assertThat(this.loggingSystem.getStandardConfigLocations()).containsExactly("log4j2-test.properties",
219+
"log4j2-test.yaml", "log4j2-test.yml", "log4j2-test.xml", "log4j2.properties", "log4j2.yaml",
220+
"log4j2.yml", "log4j2.xml");
213221
}
214222

215223
@Test
216224
void configLocationsWithJacksonDatabindAndDataformatYaml() {
217225
this.loggingSystem.availableClasses("com.fasterxml.jackson.dataformat.yaml.YAMLParser",
218226
ObjectMapper.class.getName());
219-
assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.yaml", "log4j2.yml", "log4j2.json",
220-
"log4j2.jsn", "log4j2.xml");
227+
assertThat(this.loggingSystem.getStandardConfigLocations()).containsExactly("log4j2-test.properties",
228+
"log4j2-test.yaml", "log4j2-test.yml", "log4j2-test.json", "log4j2-test.jsn", "log4j2-test.xml",
229+
"log4j2.properties", "log4j2.yaml", "log4j2.yml", "log4j2.json", "log4j2.jsn", "log4j2.xml");
221230
}
222231

223232
@Test
224233
void springConfigLocations() {
225234
String[] locations = getSpringConfigLocations(this.loggingSystem);
226-
assertThat(locations).containsExactly("log4j2-spring.properties", "log4j2-spring.xml");
235+
assertThat(locations).containsExactly("log4j2-test-spring.properties", "log4j2-test-spring.xml",
236+
"log4j2-spring.properties", "log4j2-spring.xml");
227237
}
228238

229239
@Test
230240
void exceptionsIncludeClassPackaging(CapturedOutput output) {
231241
this.loggingSystem.beforeInitialize();
232-
this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir()));
242+
this.loggingSystem.initialize(null, getRelativeClasspathLocation("log4j2-file.xml"),
243+
getLogFile(null, tmpDir()));
233244
this.logger.warn("Expected exception", new RuntimeException("Expected"));
234245
String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
235246
assertThat(fileContents).contains("[junit-");
@@ -249,7 +260,8 @@ void customExceptionConversionWord(CapturedOutput output) {
249260
try {
250261
this.loggingSystem.beforeInitialize();
251262
this.logger.info("Hidden");
252-
this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir()));
263+
this.loggingSystem.initialize(null, getRelativeClasspathLocation("log4j2-file.xml"),
264+
getLogFile(null, tmpDir()));
253265
this.logger.warn("Expected exception", new RuntimeException("Expected", new RuntimeException("Cause")));
254266
String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
255267
assertThat(fileContents).contains("java.lang.RuntimeException: Expected").doesNotContain("Wrapped by:");
@@ -278,6 +290,14 @@ void initializationIsOnlyPerformedOnceUntilCleanedUp() {
278290
verify(listener, times(4)).propertyChange(any(PropertyChangeEvent.class));
279291
}
280292

293+
private String getRelativeClasspathLocation(String fileName) {
294+
String defaultPath = ClassUtils.getPackageName(getClass());
295+
defaultPath = defaultPath.replace('.', '/');
296+
defaultPath = defaultPath + "/" + fileName;
297+
defaultPath = "classpath:" + defaultPath;
298+
return defaultPath;
299+
}
300+
281301
static class TestLog4J2LoggingSystem extends Log4J2LoggingSystem {
282302

283303
private List<String> availableClasses = new ArrayList<>();

0 commit comments

Comments
 (0)