From 64eb36b7a187886fe14dce36b66edca21a414366 Mon Sep 17 00:00:00 2001 From: Ralph Goers Date: Wed, 12 Oct 2022 11:16:58 -0700 Subject: [PATCH 1/2] Support 'log4j.configurationFile' system property Update `Log4J2LoggingSystem.getStandardConfigLocations()` so that any configured 'log4j.configurationFile' system property is also included as a location. See gh-32730 --- .../boot/logging/log4j2/Log4J2LoggingSystem.java | 7 +++++++ 1 file changed, 7 insertions(+) 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 d4bec28fb6..8835ad25c5 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 @@ -24,6 +24,7 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Properties; import java.util.Set; import java.util.logging.ConsoleHandler; import java.util.logging.Handler; @@ -45,6 +46,7 @@ import org.apache.logging.log4j.core.filter.AbstractFilter; import org.apache.logging.log4j.core.util.NameUtil; import org.apache.logging.log4j.jul.Log4jBridgeHandler; import org.apache.logging.log4j.message.Message; +import org.apache.logging.log4j.util.PropertiesUtil; import org.springframework.boot.context.properties.bind.BindResult; import org.springframework.boot.context.properties.bind.Bindable; @@ -137,6 +139,11 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem { Collections.addAll(supportedConfigLocations, "log4j2.json", "log4j2.jsn"); } supportedConfigLocations.add("log4j2.xml"); + PropertiesUtil props = new PropertiesUtil(new Properties()); + String location = props.getStringProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + if (location != null) { + supportedConfigLocations.add(location); + } return StringUtils.toStringArray(supportedConfigLocations); } From a08a6378f0dd00616125183a5f4df9cba8ede2b1 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 12 Oct 2022 11:35:47 -0700 Subject: [PATCH 2/2] Polish 'Support 'log4j.configurationFile' system property' See gh-32730 --- .../logging/log4j2/Log4J2LoggingSystem.java | 39 ++++++++----------- .../log4j2/Log4J2LoggingSystemTests.java | 13 +++++++ 2 files changed, 29 insertions(+), 23 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 8835ad25c5..f7aad8dee2 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 @@ -73,6 +73,7 @@ import org.springframework.util.StringUtils; * @author Andy Wilkinson * @author Alexander Heusingfeld * @author Ben Hale + * @author Ralph Goers * @since 1.2.0 */ public class Log4J2LoggingSystem extends AbstractLoggingSystem { @@ -125,37 +126,29 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem { @Override protected String[] getStandardConfigLocations() { - return getCurrentlySupportedConfigLocations(); - } - - private String[] getCurrentlySupportedConfigLocations() { - List supportedConfigLocations = new ArrayList<>(); - addTestFiles(supportedConfigLocations); - supportedConfigLocations.add("log4j2.properties"); + List locations = new ArrayList<>(); + locations.add("log4j2-test.properties"); if (isClassAvailable("com.fasterxml.jackson.dataformat.yaml.YAMLParser")) { - Collections.addAll(supportedConfigLocations, "log4j2.yaml", "log4j2.yml"); + Collections.addAll(locations, "log4j2-test.yaml", "log4j2-test.yml"); } if (isClassAvailable("com.fasterxml.jackson.databind.ObjectMapper")) { - Collections.addAll(supportedConfigLocations, "log4j2.json", "log4j2.jsn"); - } - supportedConfigLocations.add("log4j2.xml"); - PropertiesUtil props = new PropertiesUtil(new Properties()); - String location = props.getStringProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); - if (location != null) { - supportedConfigLocations.add(location); + Collections.addAll(locations, "log4j2-test.json", "log4j2-test.jsn"); } - return StringUtils.toStringArray(supportedConfigLocations); - } - - private void addTestFiles(List supportedConfigLocations) { - supportedConfigLocations.add("log4j2-test.properties"); + locations.add("log4j2-test.xml"); + locations.add("log4j2.properties"); if (isClassAvailable("com.fasterxml.jackson.dataformat.yaml.YAMLParser")) { - Collections.addAll(supportedConfigLocations, "log4j2-test.yaml", "log4j2-test.yml"); + Collections.addAll(locations, "log4j2.yaml", "log4j2.yml"); } if (isClassAvailable("com.fasterxml.jackson.databind.ObjectMapper")) { - Collections.addAll(supportedConfigLocations, "log4j2-test.json", "log4j2-test.jsn"); + Collections.addAll(locations, "log4j2.json", "log4j2.jsn"); + } + locations.add("log4j2.xml"); + String propertyDefinedLocation = new PropertiesUtil(new Properties()) + .getStringProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + if (propertyDefinedLocation != null) { + locations.add(propertyDefinedLocation); } - supportedConfigLocations.add("log4j2-test.xml"); + return StringUtils.toStringArray(locations); } protected boolean isClassAvailable(String className) { 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 a6ff8a1309..6045e50a3f 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 @@ -35,6 +35,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.LoggerContext; import org.apache.logging.log4j.core.config.Configuration; +import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.apache.logging.log4j.core.config.LoggerConfig; import org.apache.logging.log4j.core.config.Reconfigurable; import org.apache.logging.log4j.core.config.composite.CompositeConfiguration; @@ -295,6 +296,18 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { "log4j2.properties", "log4j2.yaml", "log4j2.yml", "log4j2.json", "log4j2.jsn", "log4j2.xml"); } + @Test + void configLocationsWithConfigurationFileSystemProperty() { + System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "custom-log4j2.properties"); + try { + assertThat(this.loggingSystem.getStandardConfigLocations()).contains("log4j2-test.properties", + "log4j2-test.xml", "log4j2.properties", "log4j2.xml"); + } + finally { + System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY); + } + } + @Test void springConfigLocations() { String[] locations = getSpringConfigLocations(this.loggingSystem);