From 43e7ccd6208a4f0422db220edace30e32fb9e823 Mon Sep 17 00:00:00 2001 From: Madhura Bhave Date: Tue, 5 May 2020 13:49:47 -0700 Subject: [PATCH] Detect log4j2-test.* files when using log4J2 Fixes gh-17001 --- .../logging/log4j2/Log4J2LoggingSystem.java | 12 ++++++ .../log4j2/Log4J2LoggingSystemTests.java | 38 ++++++++++++++----- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java index 29ffb469fc..e8748f7a55 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java @@ -109,6 +109,7 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem { private String[] getCurrentlySupportedConfigLocations() { List supportedConfigLocations = new ArrayList<>(); + addTestFiles(supportedConfigLocations); supportedConfigLocations.add("log4j2.properties"); if (isClassAvailable("com.fasterxml.jackson.dataformat.yaml.YAMLParser")) { Collections.addAll(supportedConfigLocations, "log4j2.yaml", "log4j2.yml"); @@ -120,6 +121,17 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem { return StringUtils.toStringArray(supportedConfigLocations); } + private void addTestFiles(List supportedConfigLocations) { + supportedConfigLocations.add("log4j2-test.properties"); + if (isClassAvailable("com.fasterxml.jackson.dataformat.yaml.YAMLParser")) { + Collections.addAll(supportedConfigLocations, "log4j2-test.yaml", "log4j2-test.yml"); + } + if (isClassAvailable("com.fasterxml.jackson.databind.ObjectMapper")) { + Collections.addAll(supportedConfigLocations, "log4j2-test.json", "log4j2-test.jsn"); + } + supportedConfigLocations.add("log4j2-test.xml"); + } + protected boolean isClassAvailable(String className) { return ClassUtils.isPresent(className, getClassLoader()); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java index e5a957e554..902e6b90c0 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java @@ -42,6 +42,7 @@ import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystemProperties; import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.OutputCaptureExtension; +import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -59,6 +60,7 @@ import static org.mockito.Mockito.verify; * @author Phillip Webb * @author Andy Wilkinson * @author Ben Hale + * @author Madhura Bhave */ @ExtendWith(OutputCaptureExtension.class) class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { @@ -101,7 +103,8 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { void withFile(CapturedOutput output) { this.loggingSystem.beforeInitialize(); this.logger.info("Hidden"); - this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir())); + this.loggingSystem.initialize(null, getRelativeClasspathLocation("log4j2-file.xml"), + getLogFile(null, tmpDir())); this.logger.info("Hello world"); Configuration configuration = this.loggingSystem.getConfiguration(); assertThat(output).contains("Hello world").doesNotContain("Hidden"); @@ -197,39 +200,47 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { @Test void configLocationsWithNoExtraDependencies() { - assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.properties", "log4j2.xml"); + assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2-test.properties", + "log4j2-test.xml", "log4j2.properties", "log4j2.xml"); } @Test void configLocationsWithJacksonDatabind() { this.loggingSystem.availableClasses(ObjectMapper.class.getName()); - assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.json", "log4j2.jsn", "log4j2.xml"); + assertThat(this.loggingSystem.getStandardConfigLocations()).containsExactly("log4j2-test.properties", + "log4j2-test.json", "log4j2-test.jsn", "log4j2-test.xml", "log4j2.properties", "log4j2.json", + "log4j2.jsn", "log4j2.xml"); } @Test void configLocationsWithJacksonDataformatYaml() { this.loggingSystem.availableClasses("com.fasterxml.jackson.dataformat.yaml.YAMLParser"); - assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.yaml", "log4j2.yml", "log4j2.xml"); + assertThat(this.loggingSystem.getStandardConfigLocations()).containsExactly("log4j2-test.properties", + "log4j2-test.yaml", "log4j2-test.yml", "log4j2-test.xml", "log4j2.properties", "log4j2.yaml", + "log4j2.yml", "log4j2.xml"); } @Test void configLocationsWithJacksonDatabindAndDataformatYaml() { this.loggingSystem.availableClasses("com.fasterxml.jackson.dataformat.yaml.YAMLParser", ObjectMapper.class.getName()); - assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.yaml", "log4j2.yml", "log4j2.json", - "log4j2.jsn", "log4j2.xml"); + assertThat(this.loggingSystem.getStandardConfigLocations()).containsExactly("log4j2-test.properties", + "log4j2-test.yaml", "log4j2-test.yml", "log4j2-test.json", "log4j2-test.jsn", "log4j2-test.xml", + "log4j2.properties", "log4j2.yaml", "log4j2.yml", "log4j2.json", "log4j2.jsn", "log4j2.xml"); } @Test void springConfigLocations() { String[] locations = getSpringConfigLocations(this.loggingSystem); - assertThat(locations).containsExactly("log4j2-spring.properties", "log4j2-spring.xml"); + assertThat(locations).containsExactly("log4j2-test-spring.properties", "log4j2-test-spring.xml", + "log4j2-spring.properties", "log4j2-spring.xml"); } @Test void exceptionsIncludeClassPackaging(CapturedOutput output) { this.loggingSystem.beforeInitialize(); - this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir())); + this.loggingSystem.initialize(null, getRelativeClasspathLocation("log4j2-file.xml"), + getLogFile(null, tmpDir())); this.logger.warn("Expected exception", new RuntimeException("Expected")); String fileContents = contentOf(new File(tmpDir() + "/spring.log")); assertThat(fileContents).contains("[junit-"); @@ -249,7 +260,8 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { try { this.loggingSystem.beforeInitialize(); this.logger.info("Hidden"); - this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir())); + this.loggingSystem.initialize(null, getRelativeClasspathLocation("log4j2-file.xml"), + getLogFile(null, tmpDir())); this.logger.warn("Expected exception", new RuntimeException("Expected", new RuntimeException("Cause"))); String fileContents = contentOf(new File(tmpDir() + "/spring.log")); assertThat(fileContents).contains("java.lang.RuntimeException: Expected").doesNotContain("Wrapped by:"); @@ -278,6 +290,14 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { verify(listener, times(4)).propertyChange(any(PropertyChangeEvent.class)); } + private String getRelativeClasspathLocation(String fileName) { + String defaultPath = ClassUtils.getPackageName(getClass()); + defaultPath = defaultPath.replace('.', '/'); + defaultPath = defaultPath + "/" + fileName; + defaultPath = "classpath:" + defaultPath; + return defaultPath; + } + static class TestLog4J2LoggingSystem extends Log4J2LoggingSystem { private List availableClasses = new ArrayList<>();