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/10/30 07:20:32 UTC

[logging-log4j2] branch release-2.x updated: Remove dependency on PropertiesUtil from `Strings`

This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 3a4de2b0a9 Remove dependency on PropertiesUtil from `Strings`
3a4de2b0a9 is described below

commit 3a4de2b0a92b59f02c640d915e5c22e5a0e4aff1
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Sat Oct 29 21:30:16 2022 +0200

    Remove dependency on PropertiesUtil from `Strings`
    
    The static initializer of `Strings` depends on `PropertiesUtil` to
    retrieve the system's line separator. This might introduce dangerous
    recursive dependencies between `Strings`, `PropertiesUtil`,
    `ServiceRegistry` and `ServiceLoaderUtil`.
---
 .../src/main/java/org/apache/logging/log4j/util/Strings.java |  2 +-
 .../logging/log4j/util/SystemPropertiesPropertySource.java   | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java b/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
index fa2ce33b36..f5891f199a 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/util/Strings.java
@@ -44,7 +44,7 @@ public final class Strings {
      * OS-dependent line separator, defaults to {@code "\n"} if the system property {@code ""line.separator"} cannot be
      * read.
      */
-    public static final String LINE_SEPARATOR = PropertiesUtil.getProperties().getStringProperty("line.separator",
+    public static final String LINE_SEPARATOR = SystemPropertiesPropertySource.getSystemProperty("line.separator",
             "\n");
 
     /**
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 38607e8535..392678d5eb 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
@@ -32,6 +32,18 @@ public class SystemPropertiesPropertySource implements PropertySource {
 	private static final int DEFAULT_PRIORITY = 0;
 	private static final String PREFIX = "log4j2.";
 
+	/**
+	 * Used by bootstrap code to get system properties without loading PropertiesUtil.
+	 */
+	public static String getSystemProperty(final String key, final String defaultValue) {
+		try {
+			return System.getProperty(key, defaultValue);
+		} catch (SecurityException e) {
+			// Silently ignore the exception
+			return defaultValue;
+		}
+	}
+
 	@Override
 	public int getPriority() {
 		return DEFAULT_PRIORITY;