Skip to content

Commit a08a637

Browse files
committed
Polish 'Support 'log4j.configurationFile' system property'
See gh-32730
1 parent 64eb36b commit a08a637

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

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

+16-23
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373
* @author Andy Wilkinson
7474
* @author Alexander Heusingfeld
7575
* @author Ben Hale
76+
* @author Ralph Goers
7677
* @since 1.2.0
7778
*/
7879
public class Log4J2LoggingSystem extends AbstractLoggingSystem {
@@ -125,37 +126,29 @@ public Log4J2LoggingSystem(ClassLoader classLoader) {
125126

126127
@Override
127128
protected String[] getStandardConfigLocations() {
128-
return getCurrentlySupportedConfigLocations();
129-
}
130-
131-
private String[] getCurrentlySupportedConfigLocations() {
132-
List<String> supportedConfigLocations = new ArrayList<>();
133-
addTestFiles(supportedConfigLocations);
134-
supportedConfigLocations.add("log4j2.properties");
129+
List<String> locations = new ArrayList<>();
130+
locations.add("log4j2-test.properties");
135131
if (isClassAvailable("com.fasterxml.jackson.dataformat.yaml.YAMLParser")) {
136-
Collections.addAll(supportedConfigLocations, "log4j2.yaml", "log4j2.yml");
132+
Collections.addAll(locations, "log4j2-test.yaml", "log4j2-test.yml");
137133
}
138134
if (isClassAvailable("com.fasterxml.jackson.databind.ObjectMapper")) {
139-
Collections.addAll(supportedConfigLocations, "log4j2.json", "log4j2.jsn");
140-
}
141-
supportedConfigLocations.add("log4j2.xml");
142-
PropertiesUtil props = new PropertiesUtil(new Properties());
143-
String location = props.getStringProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
144-
if (location != null) {
145-
supportedConfigLocations.add(location);
135+
Collections.addAll(locations, "log4j2-test.json", "log4j2-test.jsn");
146136
}
147-
return StringUtils.toStringArray(supportedConfigLocations);
148-
}
149-
150-
private void addTestFiles(List<String> supportedConfigLocations) {
151-
supportedConfigLocations.add("log4j2-test.properties");
137+
locations.add("log4j2-test.xml");
138+
locations.add("log4j2.properties");
152139
if (isClassAvailable("com.fasterxml.jackson.dataformat.yaml.YAMLParser")) {
153-
Collections.addAll(supportedConfigLocations, "log4j2-test.yaml", "log4j2-test.yml");
140+
Collections.addAll(locations, "log4j2.yaml", "log4j2.yml");
154141
}
155142
if (isClassAvailable("com.fasterxml.jackson.databind.ObjectMapper")) {
156-
Collections.addAll(supportedConfigLocations, "log4j2-test.json", "log4j2-test.jsn");
143+
Collections.addAll(locations, "log4j2.json", "log4j2.jsn");
144+
}
145+
locations.add("log4j2.xml");
146+
String propertyDefinedLocation = new PropertiesUtil(new Properties())
147+
.getStringProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
148+
if (propertyDefinedLocation != null) {
149+
locations.add(propertyDefinedLocation);
157150
}
158-
supportedConfigLocations.add("log4j2-test.xml");
151+
return StringUtils.toStringArray(locations);
159152
}
160153

161154
protected boolean isClassAvailable(String className) {

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

+13
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.apache.logging.log4j.Logger;
3636
import org.apache.logging.log4j.core.LoggerContext;
3737
import org.apache.logging.log4j.core.config.Configuration;
38+
import org.apache.logging.log4j.core.config.ConfigurationFactory;
3839
import org.apache.logging.log4j.core.config.LoggerConfig;
3940
import org.apache.logging.log4j.core.config.Reconfigurable;
4041
import org.apache.logging.log4j.core.config.composite.CompositeConfiguration;
@@ -295,6 +296,18 @@ void configLocationsWithJacksonDatabindAndDataformatYaml() {
295296
"log4j2.properties", "log4j2.yaml", "log4j2.yml", "log4j2.json", "log4j2.jsn", "log4j2.xml");
296297
}
297298

299+
@Test
300+
void configLocationsWithConfigurationFileSystemProperty() {
301+
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "custom-log4j2.properties");
302+
try {
303+
assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2-test.properties",
304+
"log4j2-test.xml", "log4j2.properties", "log4j2.xml");
305+
}
306+
finally {
307+
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
308+
}
309+
}
310+
298311
@Test
299312
void springConfigLocations() {
300313
String[] locations = getSpringConfigLocations(this.loggingSystem);

0 commit comments

Comments
 (0)