You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2022/06/16 12:58:33 UTC
[logging-log4j2] branch master updated: [LOG4J2-3366] Fixes order of property sources
This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/master by this push:
new c79e611e7f [LOG4J2-3366] Fixes order of property sources
c79e611e7f is described below
commit c79e611e7fa0dc05172a121388e7306c6fe877d5
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Thu Mar 10 06:48:14 2022 +0100
[LOG4J2-3366] Fixes order of property sources
Fixes the order of property sources to work as documented and changes
the priorities of the sources: more specific sources (e.g. Spring
Environment) should have priority over global settings such as
environment variables.
Conflicts:
log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
src/site/xdoc/manual/configuration.xml.vm
---
log4j-api-test/pom.xml | 4 +
.../log4j/util/PropertiesUtilOrderTest.java | 204 +++++++++++++++++++++
.../resources/PropertiesUtilOrderTest.properties | 43 +++++
.../log4j/util/EnvironmentPropertySource.java | 45 ++++-
.../log4j/util/PropertiesPropertySource.java | 27 ++-
.../apache/logging/log4j/util/PropertiesUtil.java | 95 ++++++----
.../log4j/util/PropertyFilePropertySource.java | 5 -
.../apache/logging/log4j/util/PropertySource.java | 13 +-
.../log4j/util/SystemPropertiesPropertySource.java | 26 ++-
.../log4j/spring/boot/SpringPropertySource.java | 4 +-
pom.xml | 7 +
src/site/asciidoc/manual/configuration.adoc | 30 +--
12 files changed, 446 insertions(+), 57 deletions(-)
diff --git a/log4j-api-test/pom.xml b/log4j-api-test/pom.xml
index 74826d0d30..67b2ea43b9 100644
--- a/log4j-api-test/pom.xml
+++ b/log4j-api-test/pom.xml
@@ -97,6 +97,10 @@
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
+ <dependency>
+ <groupId>uk.org.webcompere</groupId>
+ <artifactId>system-stubs-jupiter</artifactId>
+ </dependency>
</dependencies>
<build>
<plugins>
diff --git a/log4j-api-test/src/test/java/org/apache/logging/log4j/util/PropertiesUtilOrderTest.java b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/PropertiesUtilOrderTest.java
new file mode 100644
index 0000000000..6cb3831af6
--- /dev/null
+++ b/log4j-api-test/src/test/java/org/apache/logging/log4j/util/PropertiesUtilOrderTest.java
@@ -0,0 +1,204 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.logging.log4j.util;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.util.Properties;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.api.parallel.ResourceLock;
+import org.junit.jupiter.api.parallel.Resources;
+
+import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
+import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;
+import uk.org.webcompere.systemstubs.properties.SystemProperties;
+
+@ExtendWith(SystemStubsExtension.class)
+@ResourceLock(value = Resources.SYSTEM_PROPERTIES)
+public class PropertiesUtilOrderTest {
+
+ public static class NonEnumerablePropertySource implements PropertySource {
+
+ private final Properties props;
+
+ public NonEnumerablePropertySource(final Properties props) {
+ this.props = props;
+ }
+
+ @Override
+ public int getPriority() {
+ return Integer.MIN_VALUE;
+ }
+
+ @Override
+ public CharSequence getNormalForm(Iterable<? extends CharSequence> tokens) {
+ return "log4j2." + PropertySource.Util.joinAsCamelCase(tokens);
+ }
+
+ @Override
+ public String getProperty(String key) {
+ return props.getProperty(key);
+ }
+
+ @Override
+ public boolean containsProperty(String key) {
+ return getProperty(key) != null;
+ }
+
+ }
+
+ public static class NullPropertySource implements PropertySource {
+
+ @Override
+ public int getPriority() {
+ return Integer.MIN_VALUE;
+ }
+
+ }
+
+ private final Properties properties = new Properties();
+
+ @BeforeEach
+ public void setUp() throws Exception {
+ properties.load(ClassLoader.getSystemResourceAsStream("PropertiesUtilOrderTest.properties"));
+ }
+
+ @Test
+ public void normalizedOverrideLegacy() {
+ final PropertiesUtil util = new PropertiesUtil(properties);
+ final String legacy = "props.legacy";
+ final String normalized = "props.normalized";
+ assertEquals(legacy, properties.getProperty("log4j.legacyProperty"));
+ assertTrue(util.hasProperty("log4j.legacyProperty"));
+ assertEquals(normalized, util.getStringProperty("log4j.legacyProperty"));
+ assertEquals(legacy, properties.getProperty("org.apache.logging.log4j.legacyProperty2"));
+ assertTrue(util.hasProperty("log4j.legacyProperty2"));
+ assertEquals(normalized, util.getStringProperty("org.apache.logging.log4j.legacyProperty2"));
+ assertEquals(legacy, properties.getProperty("Log4jLegacyProperty3"));
+ assertTrue(util.hasProperty("log4j.legacyProperty3"));
+ assertEquals(normalized, util.getStringProperty("Log4jLegacyProperty3"));
+ // non-overridden legacy property
+ assertTrue(util.hasProperty("log4j.nonOverriddenLegacy"));
+ assertEquals(legacy, util.getStringProperty("log4j.nonOverriddenLegacy"));
+ }
+
+ @Test
+ public void fallsBackToTokenMatching() {
+ final PropertiesUtil util = new PropertiesUtil(properties);
+ for (int i = 1; i <= 4; i++) {
+ final String key = "log4j2.tokenBasedProperty" + i;
+ assertTrue(util.hasProperty(key));
+ assertEquals("props.token", util.getStringProperty(key));
+ }
+ // No fall back (a normalized property is present)
+ assertTrue(util.hasProperty("log4j2.normalizedProperty"));
+ assertEquals("props.normalized", util.getStringProperty("log4j2.normalizedProperty"));
+ }
+
+ @Test
+ public void orderOfNormalizedProperties(EnvironmentVariables env, SystemProperties sysProps) {
+ properties.remove("log4j2.normalizedProperty");
+ properties.remove("LOG4J_normalized.property");
+ final PropertiesUtil util = new PropertiesUtil(properties);
+ // Same result for both a legacy property and a normalized property
+ assertFalse(util.hasProperty("Log4jNormalizedProperty"));
+ assertEquals(null, util.getStringProperty("Log4jNormalizedProperty"));
+ assertFalse(util.hasProperty("log4j2.normalizedProperty"));
+ assertEquals(null, util.getStringProperty("log4j2.normalizedProperty"));
+
+ properties.setProperty("log4j2.normalizedProperty", "props.normalized");
+ util.reload();
+ assertTrue(util.hasProperty("Log4jNormalizedProperty"));
+ assertEquals("props.normalized", util.getStringProperty("Log4jNormalizedProperty"));
+ assertTrue(util.hasProperty("log4j2.normalizedProperty"));
+ assertEquals("props.normalized", util.getStringProperty("log4j2.normalizedProperty"));
+
+ env.set("LOG4J_NORMALIZED_PROPERTY", "env");
+ util.reload();
+ assertTrue(util.hasProperty("Log4jNormalizedProperty"));
+ assertEquals("env", util.getStringProperty("Log4jNormalizedProperty"));
+ assertTrue(util.hasProperty("log4j2.normalizedProperty"));
+ assertEquals("env", util.getStringProperty("log4j2.normalizedProperty"));
+
+ sysProps.set("log4j2.normalizedProperty", "sysProps");
+ util.reload();
+ assertTrue(util.hasProperty("Log4jNormalizedProperty"));
+ assertEquals("sysProps", util.getStringProperty("Log4jNormalizedProperty"));
+ assertTrue(util.hasProperty("log4j2.normalizedProperty"));
+ assertEquals("sysProps", util.getStringProperty("log4j2.normalizedProperty"));
+ }
+
+ @Test
+ public void highPriorityNonEnumerableSource(SystemProperties sysProps) {
+ // In both datasources
+ assertNotNull(properties.getProperty("log4j2.normalizedProperty"));
+ assertNotNull(properties.getProperty("log4j.onlyLegacy"));
+ sysProps.set("log4j2.normalizedProperty", "sysProps.normalized");
+ sysProps.set("log4j.onlyLegacy", "sysProps.legazy");
+ // Only system properties
+ assertNull(properties.getProperty("log4j2.normalizedPropertySysProps"));
+ assertNull(properties.getProperty("log4j.onlyLegacySysProps"));
+ sysProps.set("log4j2.normalizedPropertySysProps", "sysProps.normalized");
+ sysProps.set("log4j.onlyLegacySysProps", "sysProps.legacy");
+ // Only the non enumerable source
+ assertNotNull(properties.getProperty("log4j2.normalizedPropertyProps"));
+ assertNotNull(properties.getProperty("log4j.onlyLegacyProps"));
+
+ final PropertiesUtil util = new PropertiesUtil(new NonEnumerablePropertySource(properties));
+ assertTrue(util.hasProperty("log4j2.normalizedProperty"));
+ assertEquals("props.normalized", util.getStringProperty("log4j2.normalizedProperty"));
+ assertTrue(util.hasProperty("log4j.onlyLegacy"));
+ assertEquals("props.legacy", util.getStringProperty("log4j.onlyLegacy"));
+ assertTrue(util.hasProperty("log4j2.normalizedPropertySysProps"));
+ assertEquals("sysProps.normalized", util.getStringProperty("log4j2.normalizedPropertySysProps"));
+ assertTrue(util.hasProperty("log4j.onlyLegacySysProps"));
+ assertEquals("sysProps.legacy", util.getStringProperty("log4j.onlyLegacySysProps"));
+ assertTrue(util.hasProperty("log4j2.normalizedPropertyProps"));
+ assertEquals("props.normalized", util.getStringProperty("log4j2.normalizedPropertyProps"));
+ assertTrue(util.hasProperty("log4j.onlyLegacyProps"));
+ assertEquals("props.legacy", util.getStringProperty("log4j.onlyLegacyProps"));
+ }
+
+ /**
+ * Checks the for missing null checks. The {@link NullPropertySource} returns
+ * {@code null} in almost every call.
+ *
+ * @param sysProps
+ */
+ @Test
+ public void nullChecks(SystemProperties sysProps) {
+ sysProps.set("log4j2.someProperty", "sysProps");
+ sysProps.set("Log4jLegacyProperty", "sysProps");
+ final PropertiesUtil util = new PropertiesUtil(new NullPropertySource());
+ assertTrue(util.hasProperty("log4j2.someProperty"));
+ assertEquals("sysProps", util.getStringProperty("log4j2.someProperty"));
+ assertTrue(util.hasProperty("Log4jLegacyProperty"));
+ assertEquals("sysProps", util.getStringProperty("Log4jLegacyProperty"));
+ assertTrue(util.hasProperty("log4j.legacyProperty"));
+ assertEquals("sysProps", util.getStringProperty("log4j.legacyProperty"));
+ assertFalse(util.hasProperty("log4j2.nonExistentProperty"));
+ assertNull(util.getStringProperty("log4j2.nonExistentProperty"));
+ }
+}
diff --git a/log4j-api-test/src/test/resources/PropertiesUtilOrderTest.properties b/log4j-api-test/src/test/resources/PropertiesUtilOrderTest.properties
new file mode 100644
index 0000000000..64de072612
--- /dev/null
+++ b/log4j-api-test/src/test/resources/PropertiesUtilOrderTest.properties
@@ -0,0 +1,43 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements. See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache license, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the license for the specific language governing permissions and
+# limitations under the license.
+#
+
+###
+# Legacy properties
+log4j.legacyProperty=props.legacy
+org.apache.logging.log4j.legacyProperty2=props.legacy
+Log4jLegacyProperty3=props.legacy
+log4j.nonOverriddenLegacy=props.legacy
+log4j.onlyLegacy=props.legacy
+log4j.onlyLegacyProps=props.legacy
+
+###
+# Their equivalent normalized versions
+log4j2.legacyProperty=props.normalized
+log4j2.legacyProperty2=props.normalized
+log4j2.legacyProperty3=props.normalized
+
+###
+# Token-based matching
+LOG4J_token.based.property1=props.token
+LOG4J_token-based-property2=props.token
+LOG4J_token/based/property3=props.token
+LOG4J_tokenBasedProperty4=props.token
+
+##
+LOG4J_normalized.property=props.token
+log4j2.normalizedProperty=props.normalized
+log4j2.normalizedPropertyProps=props.normalized
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/EnvironmentPropertySource.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/EnvironmentPropertySource.java
index f2ef0df998..2d93a4c6e2 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/EnvironmentPropertySource.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/EnvironmentPropertySource.java
@@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j.util;
+import java.util.Collection;
import java.util.Map;
/**
@@ -30,23 +31,26 @@ import java.util.Map;
public class EnvironmentPropertySource implements PropertySource {
private static final String PREFIX = "LOG4J_";
- private static final int DEFAULT_PRIORITY = -100;
+ private static final int DEFAULT_PRIORITY = 100;
@Override
public int getPriority() {
return DEFAULT_PRIORITY;
}
+ private void logException(SecurityException e) {
+ // There is no status logger yet.
+ LowLevelLogUtil.logException(
+ "The system environment variables are not available to Log4j due to security restrictions: " + e, e);
+ }
+
@Override
public void forEach(final BiConsumer<String, String> action) {
final Map<String, String> getenv;
try {
getenv = System.getenv();
} catch (final SecurityException e) {
- // There is no status logger yet.
- LowLevelLogUtil.logException(
- "The system environment variables are not available to Log4j due to security restrictions: " + e,
- e);
+ logException(e);
return;
}
for (final Map.Entry<String, String> entry : getenv.entrySet()) {
@@ -68,4 +72,35 @@ public class EnvironmentPropertySource implements PropertySource {
}
return sb.toString();
}
+
+ @Override
+ public Collection<String> getPropertyNames() {
+ try {
+ return System.getenv().keySet();
+ } catch (final SecurityException e) {
+ logException(e);
+ return PropertySource.super.getPropertyNames();
+ }
+ }
+
+ @Override
+ public String getProperty(String key) {
+ try {
+ return System.getenv(key);
+ } catch (final SecurityException e) {
+ logException(e);
+ return PropertySource.super.getProperty(key);
+ }
+ }
+
+ @Override
+ public boolean containsProperty(String key) {
+ try {
+ return System.getenv().containsKey(key);
+ } catch (final SecurityException e) {
+ logException(e);
+ return PropertySource.super.containsProperty(key);
+ }
+ }
+
}
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesPropertySource.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesPropertySource.java
index 6f3e8f44b2..7d732a23fb 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesPropertySource.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesPropertySource.java
@@ -16,6 +16,8 @@
*/
package org.apache.logging.log4j.util;
+import java.util.Collection;
+import java.util.Collections;
import java.util.Map;
import java.util.Properties;
@@ -27,17 +29,24 @@ import java.util.Properties;
*/
public class PropertiesPropertySource implements PropertySource {
+ private static final int DEFAULT_PRIORITY = 200;
private static final String PREFIX = "log4j2.";
private final Properties properties;
+ private final int priority;
public PropertiesPropertySource(final Properties properties) {
+ this(properties, DEFAULT_PRIORITY);
+ }
+
+ public PropertiesPropertySource(final Properties properties, final int priority) {
this.properties = properties;
+ this.priority = priority;
}
@Override
public int getPriority() {
- return 0;
+ return priority;
}
@Override
@@ -51,4 +60,20 @@ public class PropertiesPropertySource implements PropertySource {
public CharSequence getNormalForm(final Iterable<? extends CharSequence> tokens) {
return PREFIX + Util.joinAsCamelCase(tokens);
}
+
+ @Override
+ public Collection<String> getPropertyNames() {
+ return properties.stringPropertyNames();
+ }
+
+ @Override
+ public String getProperty(String key) {
+ return properties.getProperty(key);
+ }
+
+ @Override
+ public boolean containsProperty(String key) {
+ return getProperty(key) != null;
+ }
+
}
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
index f891ded88d..453f72354c 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java
@@ -24,8 +24,10 @@ import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
import java.util.ArrayList;
import java.util.Collections;
+import java.util.HashSet;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.Properties;
import java.util.ResourceBundle;
import java.util.ServiceLoader;
@@ -61,7 +63,7 @@ public final class PropertiesUtil {
* @param props the Properties to use by default
*/
public PropertiesUtil(final Properties props) {
- this.environment = new Environment(new PropertiesPropertySource(props));
+ this(new PropertiesPropertySource(props));
}
/**
@@ -74,6 +76,14 @@ public final class PropertiesUtil {
this.environment = new Environment(new PropertyFilePropertySource(propertiesFileName));
}
+ /**
+ * Constructs a PropertiesUtil for a give property source as source of additional properties.
+ * @param source a property source
+ */
+ PropertiesUtil(final PropertySource source) {
+ this.environment = new Environment(source);
+ }
+
/**
* Loads and closes the given property input stream. If an error occurs, log to the status logger.
*
@@ -421,8 +431,15 @@ public final class PropertiesUtil {
private static class Environment {
private final Set<PropertySource> sources = new TreeSet<>(new PropertySource.Comparator());
- private final Map<CharSequence, String> literal = new ConcurrentHashMap<>();
- private final Map<CharSequence, String> normalized = new ConcurrentHashMap<>();
+ /**
+ * Maps a key to its value in the lowest priority source that contains it.
+ */
+ private final Map<String, String> literal = new ConcurrentHashMap<>();
+ /**
+ * Maps a key to the value associated to its normalization in the lowest
+ * priority source that contains it.
+ */
+ private final Map<String, String> normalized = new ConcurrentHashMap<>();
private final Map<List<CharSequence>, String> tokenized = new ConcurrentHashMap<>();
private Environment(final PropertySource propertySource) {
@@ -448,28 +465,33 @@ public final class PropertiesUtil {
literal.clear();
normalized.clear();
tokenized.clear();
- for (final PropertySource source : sources) {
- source.forEach((key, value) -> {
- if (key != null && value != null) {
- literal.put(key, value);
- final List<CharSequence> tokens = PropertySource.Util.tokenize(key);
- if (tokens.isEmpty()) {
- normalized.put(source.getNormalForm(Collections.singleton(key)), value);
- } else {
- normalized.put(source.getNormalForm(tokens), value);
- tokenized.put(tokens, value);
+ // 1. Collects all property keys from enumerable sources.
+ final Set<String> keys = new HashSet<>();
+ sources.stream()
+ .map(PropertySource::getPropertyNames)
+ .forEach(sourceKeys -> keys.addAll(sourceKeys));
+ // 2. Fills the property caches. Sources with higher priority values don't override the previous ones.
+ keys.stream()
+ .filter(Objects::nonNull)
+ .forEach(key -> {
+ final List<CharSequence> tokens = PropertySource.Util.tokenize(key);
+ sources.forEach(source -> {
+ final String value = source.getProperty(key);
+ if (value != null) {
+ literal.putIfAbsent(key, value);
+ if (!tokens.isEmpty()) {
+ tokenized.putIfAbsent(tokens, value);
+ }
}
- }
+ final CharSequence normalKey = source.getNormalForm(tokens);
+ if (normalKey != null) {
+ final String normalValue = source.getProperty(normalKey.toString());
+ if (normalValue != null) {
+ normalized.putIfAbsent(key, normalValue);
+ }
+ }
+ });
});
- }
- }
-
- private static boolean hasSystemProperty(final String key) {
- try {
- return System.getProperties().containsKey(key);
- } catch (final SecurityException ignored) {
- return false;
- }
}
private String get(final String key) {
@@ -479,22 +501,33 @@ public final class PropertiesUtil {
if (literal.containsKey(key)) {
return literal.get(key);
}
- if (hasSystemProperty(key)) {
- return System.getProperty(key);
- }
+ final List<CharSequence> tokens = PropertySource.Util.tokenize(key);
for (final PropertySource source : sources) {
+ final String normalKey = Objects.toString(source.getNormalForm(tokens), null);
+ if (normalKey != null && source.containsProperty(normalKey)) {
+ final String normalValue = source.getProperty(normalKey);
+ // Caching previously unknown keys breaks many tests which set and unset system properties
+ // normalized.put(key, normalValue);
+ return normalValue;
+ }
if (source.containsProperty(key)) {
- return source.getProperty(key);
+ final String value = source.getProperty(key);
+ // literal.put(key, value);
+ return value;
}
}
- return tokenized.get(PropertySource.Util.tokenize(key));
+ return tokenized.get(tokens);
}
private boolean containsKey(final String key) {
+ List<CharSequence> tokens = PropertySource.Util.tokenize(key);
return normalized.containsKey(key) ||
- literal.containsKey(key) ||
- hasSystemProperty(key) ||
- tokenized.containsKey(PropertySource.Util.tokenize(key));
+ literal.containsKey(key) ||
+ tokenized.containsKey(tokens) ||
+ sources.stream().anyMatch(s -> {
+ final CharSequence normalizedKey = s.getNormalForm(tokens);
+ return s.containsProperty(key) || normalizedKey != null && s.containsProperty(normalizedKey.toString());
+ });
}
}
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertyFilePropertySource.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertyFilePropertySource.java
index c4cf3596a0..bb8ccd7319 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertyFilePropertySource.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertyFilePropertySource.java
@@ -44,9 +44,4 @@ public class PropertyFilePropertySource extends PropertiesPropertySource {
return props;
}
- @Override
- public int getPriority() {
- return 0;
- }
-
}
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertySource.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertySource.java
index 2b12f431af..4a3e29f074 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertySource.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/PropertySource.java
@@ -19,6 +19,8 @@ package org.apache.logging.log4j.util;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -35,7 +37,7 @@ public interface PropertySource {
/**
* Returns the order in which this PropertySource has priority. A higher value means that the source will be
- * applied later so as to take precedence over other property sources.
+ * searched later and can be overridden by other property sources.
*
* @return priority value
*/
@@ -49,6 +51,15 @@ public interface PropertySource {
default void forEach(final BiConsumer<String, String> action) {
}
+ /**
+ * Returns the list of all property names.
+ *
+ * @return list of property names
+ */
+ default Collection<String> getPropertyNames() {
+ return Collections.emptySet();
+ }
+
/**
* Converts a list of property name tokens into a normal form. For example, a list of tokens such as
* "foo", "bar", "baz", might be normalized into the property name "log4j2.fooBarBaz".
diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/SystemPropertiesPropertySource.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/SystemPropertiesPropertySource.java
index 72a6cd1915..5f84836006 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/SystemPropertiesPropertySource.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/SystemPropertiesPropertySource.java
@@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j.util;
+import java.util.Collection;
import java.util.Objects;
import java.util.Properties;
@@ -28,7 +29,7 @@ import java.util.Properties;
*/
public class SystemPropertiesPropertySource implements PropertySource {
- private static final int DEFAULT_PRIORITY = 100;
+ private static final int DEFAULT_PRIORITY = 0;
private static final String PREFIX = "log4j2.";
@Override
@@ -68,4 +69,27 @@ public class SystemPropertiesPropertySource implements PropertySource {
return PREFIX + Util.joinAsCamelCase(tokens);
}
+ @Override
+ public Collection<String> getPropertyNames() {
+ try {
+ return System.getProperties().stringPropertyNames();
+ } catch (final SecurityException e) {
+ return PropertySource.super.getPropertyNames();
+ }
+ }
+
+ @Override
+ public String getProperty(String key) {
+ try {
+ return System.getProperty(key);
+ } catch (final SecurityException e) {
+ return PropertySource.super.getProperty(key);
+ }
+ }
+
+ @Override
+ public boolean containsProperty(String key) {
+ return getProperty(key) != null;
+ }
+
}
diff --git a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringPropertySource.java b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringPropertySource.java
index 6d4ddaafbf..4d6712b4c7 100644
--- a/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringPropertySource.java
+++ b/log4j-spring-boot/src/main/java/org/apache/logging/log4j/spring/boot/SpringPropertySource.java
@@ -24,6 +24,8 @@ import org.springframework.core.env.Environment;
*/
public class SpringPropertySource extends SpringEnvironmentHolder implements PropertySource {
+ private static final int DEFAULT_PRIORITY = -100;
+
/**
* System properties take precedence followed by properties in Log4j properties files. Spring properties
* follow.
@@ -31,7 +33,7 @@ public class SpringPropertySource extends SpringEnvironmentHolder implements Pro
*/
@Override
public int getPriority() {
- return -50;
+ return DEFAULT_PRIORITY ;
}
@Override
diff --git a/pom.xml b/pom.xml
index daf4b6e6c8..deb06c82ff 100644
--- a/pom.xml
+++ b/pom.xml
@@ -661,6 +661,13 @@
<artifactId>junit-jupiter-params</artifactId>
<version>${junitJupiterVersion}</version>
</dependency>
+ <!-- Environment and system properties support for Jupiter -->
+ <dependency>
+ <groupId>uk.org.webcompere</groupId>
+ <artifactId>system-stubs-jupiter</artifactId>
+ <version>2.0.1</version>
+ <scope>test</scope>
+ </dependency>
<!-- JUnit 4 API dependency -->
<dependency>
<groupId>junit</groupId>
diff --git a/src/site/asciidoc/manual/configuration.adoc b/src/site/asciidoc/manual/configuration.adoc
index 70a1879757..60fea2a03a 100644
--- a/src/site/asciidoc/manual/configuration.adoc
+++ b/src/site/asciidoc/manual/configuration.adoc
@@ -1877,8 +1877,21 @@ by default:
|===
|Source |Priority |Description
-|Environment Variables
+|Spring Boot Properties
|-100
+|This property source is enabled only if the Java application uses Spring Boot and the
+`log4j-spring` module is present. It resolves properties using a Spring
+link:https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/Environment.html[Environment].
+
+|System Properties
+|0
+|All properties can be set using normal system
+property patterns. These have the lowest numerical priority among commonly available property sources
+and can override included `log4j2.component.properties` files or environment variables. If a `log4j2.system.properties` file is available on the classpath its contents are
+sourced into Java system properties at Log4j startup.
+
+|Environment Variables
+|100
|Environment variables are all prefixed
with `LOG4J_`, are in all caps, and words are all separated by
underscores. Only this naming scheme is support for environment
@@ -1886,17 +1899,10 @@ variables as there were no old naming schemes to maintain compatibility
with.
|`log4j2.component.properties` file
-|0
-|Including this file on the
-classpath can be used as an alternative to providing properties as
-system properties. This has priority over system properties, but they
-can be overridden by environment variables as described above.
-
-|System Properties
-|100
-|All properties can be set using normal system
-property patterns. These have the lowest priority and can be overridden
-by included properties files or environment variables.
+|200
+|Including this file on the classpath can be used as an alternative to providing properties as system
+properties. This is the property source with highest numerical priority and can be used to provide
+default values that can be overridden by the system administrator.
|===
The following is a list of available global configuration properties.