Merge branch '2.2.x'

Closes gh-21323
pull/21361/head
Madhura Bhave 5 years ago
commit f23d0501e4

@ -109,6 +109,7 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
private String[] getCurrentlySupportedConfigLocations() { private String[] getCurrentlySupportedConfigLocations() {
List<String> supportedConfigLocations = new ArrayList<>(); List<String> supportedConfigLocations = new ArrayList<>();
addTestFiles(supportedConfigLocations);
supportedConfigLocations.add("log4j2.properties"); supportedConfigLocations.add("log4j2.properties");
if (isClassAvailable("com.fasterxml.jackson.dataformat.yaml.YAMLParser")) { if (isClassAvailable("com.fasterxml.jackson.dataformat.yaml.YAMLParser")) {
Collections.addAll(supportedConfigLocations, "log4j2.yaml", "log4j2.yml"); Collections.addAll(supportedConfigLocations, "log4j2.yaml", "log4j2.yml");
@ -120,6 +121,17 @@ public class Log4J2LoggingSystem extends Slf4JLoggingSystem {
return StringUtils.toStringArray(supportedConfigLocations); return StringUtils.toStringArray(supportedConfigLocations);
} }
private void addTestFiles(List<String> 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) { protected boolean isClassAvailable(String className) {
return ClassUtils.isPresent(className, getClassLoader()); return ClassUtils.isPresent(className, getClassLoader());
} }

@ -44,6 +44,7 @@ import org.springframework.boot.logging.LoggingSystem;
import org.springframework.boot.logging.LoggingSystemProperties; import org.springframework.boot.logging.LoggingSystemProperties;
import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.CapturedOutput;
import org.springframework.boot.testsupport.system.OutputCaptureExtension; import org.springframework.boot.testsupport.system.OutputCaptureExtension;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -61,6 +62,7 @@ import static org.mockito.Mockito.verify;
* @author Phillip Webb * @author Phillip Webb
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Ben Hale * @author Ben Hale
* @author Madhura Bhave
*/ */
@ExtendWith(OutputCaptureExtension.class) @ExtendWith(OutputCaptureExtension.class)
class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@ -103,7 +105,8 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
void withFile(CapturedOutput output) { void withFile(CapturedOutput output) {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.logger.info("Hidden"); 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"); this.logger.info("Hello world");
Configuration configuration = this.loggingSystem.getConfiguration(); Configuration configuration = this.loggingSystem.getConfiguration();
assertThat(output).contains("Hello world").doesNotContain("Hidden"); assertThat(output).contains("Hello world").doesNotContain("Hidden");
@ -201,39 +204,47 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@Test @Test
void configLocationsWithNoExtraDependencies() { 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 @Test
void configLocationsWithJacksonDatabind() { void configLocationsWithJacksonDatabind() {
this.loggingSystem.availableClasses(ObjectMapper.class.getName()); 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 @Test
void configLocationsWithJacksonDataformatYaml() { void configLocationsWithJacksonDataformatYaml() {
this.loggingSystem.availableClasses("com.fasterxml.jackson.dataformat.yaml.YAMLParser"); 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 @Test
void configLocationsWithJacksonDatabindAndDataformatYaml() { void configLocationsWithJacksonDatabindAndDataformatYaml() {
this.loggingSystem.availableClasses("com.fasterxml.jackson.dataformat.yaml.YAMLParser", this.loggingSystem.availableClasses("com.fasterxml.jackson.dataformat.yaml.YAMLParser",
ObjectMapper.class.getName()); ObjectMapper.class.getName());
assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.yaml", "log4j2.yml", "log4j2.json", assertThat(this.loggingSystem.getStandardConfigLocations()).containsExactly("log4j2-test.properties",
"log4j2.jsn", "log4j2.xml"); "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 @Test
void springConfigLocations() { void springConfigLocations() {
String[] locations = getSpringConfigLocations(this.loggingSystem); 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 @Test
void exceptionsIncludeClassPackaging(CapturedOutput output) { void exceptionsIncludeClassPackaging(CapturedOutput output) {
this.loggingSystem.beforeInitialize(); 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")); this.logger.warn("Expected exception", new RuntimeException("Expected"));
String fileContents = contentOf(new File(tmpDir() + "/spring.log")); String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
assertThat(fileContents).contains("[junit-"); assertThat(fileContents).contains("[junit-");
@ -253,7 +264,8 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
try { try {
this.loggingSystem.beforeInitialize(); this.loggingSystem.beforeInitialize();
this.logger.info("Hidden"); 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"))); this.logger.warn("Expected exception", new RuntimeException("Expected", new RuntimeException("Cause")));
String fileContents = contentOf(new File(tmpDir() + "/spring.log")); String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
assertThat(fileContents).contains("java.lang.RuntimeException: Expected").doesNotContain("Wrapped by:"); assertThat(fileContents).contains("java.lang.RuntimeException: Expected").doesNotContain("Wrapped by:");
@ -282,6 +294,14 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
verify(listener, times(4)).propertyChange(any(PropertyChangeEvent.class)); 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 { static class TestLog4J2LoggingSystem extends Log4J2LoggingSystem {
private List<String> availableClasses = new ArrayList<>(); private List<String> availableClasses = new ArrayList<>();

Loading…
Cancel
Save