Skip to content

Commit f23d050

Browse files
committed
Merge branch '2.2.x'
Closes gh-21323
2 parents 3325709 + 43e7ccd commit f23d050

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
@@ -44,6 +44,7 @@
4444
import org.springframework.boot.logging.LoggingSystemProperties;
4545
import org.springframework.boot.testsupport.system.CapturedOutput;
4646
import org.springframework.boot.testsupport.system.OutputCaptureExtension;
47+
import org.springframework.util.ClassUtils;
4748
import org.springframework.util.StringUtils;
4849

4950
import static org.assertj.core.api.Assertions.assertThat;
@@ -61,6 +62,7 @@
6162
* @author Phillip Webb
6263
* @author Andy Wilkinson
6364
* @author Ben Hale
65+
* @author Madhura Bhave
6466
*/
6567
@ExtendWith(OutputCaptureExtension.class)
6668
class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests {
@@ -103,7 +105,8 @@ void noFile(CapturedOutput output) {
103105
void withFile(CapturedOutput output) {
104106
this.loggingSystem.beforeInitialize();
105107
this.logger.info("Hidden");
106-
this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir()));
108+
this.loggingSystem.initialize(null, getRelativeClasspathLocation("log4j2-file.xml"),
109+
getLogFile(null, tmpDir()));
107110
this.logger.info("Hello world");
108111
Configuration configuration = this.loggingSystem.getConfiguration();
109112
assertThat(output).contains("Hello world").doesNotContain("Hidden");
@@ -201,39 +204,47 @@ void loggingThatUsesJulIsCaptured(CapturedOutput output) {
201204

202205
@Test
203206
void configLocationsWithNoExtraDependencies() {
204-
assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.properties", "log4j2.xml");
207+
assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2-test.properties",
208+
"log4j2-test.xml", "log4j2.properties", "log4j2.xml");
205209
}
206210

207211
@Test
208212
void configLocationsWithJacksonDatabind() {
209213
this.loggingSystem.availableClasses(ObjectMapper.class.getName());
210-
assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.json", "log4j2.jsn", "log4j2.xml");
214+
assertThat(this.loggingSystem.getStandardConfigLocations()).containsExactly("log4j2-test.properties",
215+
"log4j2-test.json", "log4j2-test.jsn", "log4j2-test.xml", "log4j2.properties", "log4j2.json",
216+
"log4j2.jsn", "log4j2.xml");
211217
}
212218

213219
@Test
214220
void configLocationsWithJacksonDataformatYaml() {
215221
this.loggingSystem.availableClasses("com.fasterxml.jackson.dataformat.yaml.YAMLParser");
216-
assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.yaml", "log4j2.yml", "log4j2.xml");
222+
assertThat(this.loggingSystem.getStandardConfigLocations()).containsExactly("log4j2-test.properties",
223+
"log4j2-test.yaml", "log4j2-test.yml", "log4j2-test.xml", "log4j2.properties", "log4j2.yaml",
224+
"log4j2.yml", "log4j2.xml");
217225
}
218226

219227
@Test
220228
void configLocationsWithJacksonDatabindAndDataformatYaml() {
221229
this.loggingSystem.availableClasses("com.fasterxml.jackson.dataformat.yaml.YAMLParser",
222230
ObjectMapper.class.getName());
223-
assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2.yaml", "log4j2.yml", "log4j2.json",
224-
"log4j2.jsn", "log4j2.xml");
231+
assertThat(this.loggingSystem.getStandardConfigLocations()).containsExactly("log4j2-test.properties",
232+
"log4j2-test.yaml", "log4j2-test.yml", "log4j2-test.json", "log4j2-test.jsn", "log4j2-test.xml",
233+
"log4j2.properties", "log4j2.yaml", "log4j2.yml", "log4j2.json", "log4j2.jsn", "log4j2.xml");
225234
}
226235

227236
@Test
228237
void springConfigLocations() {
229238
String[] locations = getSpringConfigLocations(this.loggingSystem);
230-
assertThat(locations).containsExactly("log4j2-spring.properties", "log4j2-spring.xml");
239+
assertThat(locations).containsExactly("log4j2-test-spring.properties", "log4j2-test-spring.xml",
240+
"log4j2-spring.properties", "log4j2-spring.xml");
231241
}
232242

233243
@Test
234244
void exceptionsIncludeClassPackaging(CapturedOutput output) {
235245
this.loggingSystem.beforeInitialize();
236-
this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir()));
246+
this.loggingSystem.initialize(null, getRelativeClasspathLocation("log4j2-file.xml"),
247+
getLogFile(null, tmpDir()));
237248
this.logger.warn("Expected exception", new RuntimeException("Expected"));
238249
String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
239250
assertThat(fileContents).contains("[junit-");
@@ -253,7 +264,8 @@ void customExceptionConversionWord(CapturedOutput output) {
253264
try {
254265
this.loggingSystem.beforeInitialize();
255266
this.logger.info("Hidden");
256-
this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir()));
267+
this.loggingSystem.initialize(null, getRelativeClasspathLocation("log4j2-file.xml"),
268+
getLogFile(null, tmpDir()));
257269
this.logger.warn("Expected exception", new RuntimeException("Expected", new RuntimeException("Cause")));
258270
String fileContents = contentOf(new File(tmpDir() + "/spring.log"));
259271
assertThat(fileContents).contains("java.lang.RuntimeException: Expected").doesNotContain("Wrapped by:");
@@ -282,6 +294,14 @@ void initializationIsOnlyPerformedOnceUntilCleanedUp() {
282294
verify(listener, times(4)).propertyChange(any(PropertyChangeEvent.class));
283295
}
284296

297+
private String getRelativeClasspathLocation(String fileName) {
298+
String defaultPath = ClassUtils.getPackageName(getClass());
299+
defaultPath = defaultPath.replace('.', '/');
300+
defaultPath = defaultPath + "/" + fileName;
301+
defaultPath = "classpath:" + defaultPath;
302+
return defaultPath;
303+
}
304+
285305
static class TestLog4J2LoggingSystem extends Log4J2LoggingSystem {
286306

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

0 commit comments

Comments
 (0)