You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by rg...@apache.org on 2021/12/05 20:11:44 UTC

[logging-log4j2] branch release-2.x updated: LOG4J2-2951 - Log4j 1.x properties were not being substrituted.

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

rgoers 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 67cdd99  LOG4J2-2951 - Log4j 1.x properties were not being substrituted.
67cdd99 is described below

commit 67cdd9996bdd2bf53178c66000af0a17cf98152a
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sun Dec 5 13:10:41 2021 -0700

    LOG4J2-2951 - Log4j 1.x properties were not being substrituted.
---
 .../org/apache/log4j/builders/AbstractBuilder.java | 17 ++++--
 .../PropertiesRollingWithPropertiesTest.java       | 60 ++++++++++++++++++++++
 .../resources/log4j1-rolling-properties.properties | 31 +++++++++++
 src/changes/changes.xml                            |  3 ++
 4 files changed, 107 insertions(+), 4 deletions(-)

diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/builders/AbstractBuilder.java b/log4j-1.2-api/src/main/java/org/apache/log4j/builders/AbstractBuilder.java
index 9967ab4..35c44fb 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/builders/AbstractBuilder.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/builders/AbstractBuilder.java
@@ -24,10 +24,13 @@ import org.apache.logging.log4j.Level;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.core.filter.CompositeFilter;
 import org.apache.logging.log4j.core.filter.ThresholdFilter;
+import org.apache.logging.log4j.core.lookup.StrSubstitutor;
 import org.apache.logging.log4j.status.StatusLogger;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Properties;
 
 /**
@@ -46,31 +49,37 @@ public abstract class AbstractBuilder {
 
     private final String prefix;
     private final Properties props;
+    private final StrSubstitutor strSubstitutor;
 
     public AbstractBuilder() {
         this.prefix = null;
         this.props = new Properties();
+        strSubstitutor = new StrSubstitutor(System.getProperties());
     }
 
     public AbstractBuilder(String prefix, Properties props) {
         this.prefix = prefix + ".";
         this.props = props;
+        Map<String, String> map = new HashMap<>();
+        System.getProperties().forEach((k, v) -> map.put(k.toString(), v.toString()));
+        props.forEach((k, v) -> map.put(k.toString(), v.toString()));
+        strSubstitutor = new StrSubstitutor(map);
     }
 
     public String getProperty(String key) {
-        return props.getProperty(prefix + key);
+        return strSubstitutor.replace(props.getProperty(prefix + key));
     }
 
     public String getProperty(String key, String defaultValue) {
-        return props.getProperty(prefix + key, defaultValue);
+        return strSubstitutor.replace(props.getProperty(prefix + key, defaultValue));
     }
 
     public boolean getBooleanProperty(String key) {
-        return Boolean.parseBoolean(props.getProperty(prefix + key, Boolean.FALSE.toString()));
+        return Boolean.parseBoolean(strSubstitutor.replace(props.getProperty(prefix + key, Boolean.FALSE.toString())));
     }
 
     public int getIntegerProperty(String key, int defaultValue) {
-        String value = props.getProperty(key);
+        String value = getProperty(key);
         try {
             if (value != null) {
                 return Integer.parseInt(value);
diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/config/PropertiesRollingWithPropertiesTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/config/PropertiesRollingWithPropertiesTest.java
new file mode 100644
index 0000000..2d84f9e
--- /dev/null
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/config/PropertiesRollingWithPropertiesTest.java
@@ -0,0 +1,60 @@
+/*
+ * 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.log4j.config;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.log4j.ListAppender;
+import org.apache.log4j.LogManager;
+import org.apache.log4j.Logger;
+import org.apache.log4j.bridge.AppenderAdapter;
+import org.apache.log4j.spi.LoggingEvent;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.config.ConfigurationSource;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Test configuration from Properties.
+ */
+public class PropertiesRollingWithPropertiesTest {
+
+    @BeforeClass
+    public static void setupClass() {
+        System.setProperty("log4j.configuration", "target/test-classes/log4j1-rolling-properties.properties");
+    }
+
+    @Test
+    public void testProperties() throws Exception {
+        Logger logger = LogManager.getLogger("test");
+        logger.debug("This is a test of the root logger");
+        File file = new File("target/rolling/somefile.log");
+        assertTrue("Log file was not created", file.exists());
+        assertTrue("Log file is empty", file.length() > 0);
+    }
+
+}
diff --git a/log4j-1.2-api/src/test/resources/log4j1-rolling-properties.properties b/log4j-1.2-api/src/test/resources/log4j1-rolling-properties.properties
new file mode 100644
index 0000000..a4a1cd5
--- /dev/null
+++ b/log4j-1.2-api/src/test/resources/log4j1-rolling-properties.properties
@@ -0,0 +1,31 @@
+# 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.
+
+log4j.debug=true
+
+# Properties for substitution
+somelogfile=target/rolling/somefile.log
+maxfilesize=256MB
+maxbackupindex=20
+
+log4j.rootLogger=TRACE, RFA
+
+# Appender configuration with variables
+log4j.appender.RFA=org.apache.log4j.RollingFileAppender
+log4j.appender.RFA.File=${somelogfile}
+log4j.appender.RFA.MaxFileSize=${maxfilesize}
+log4j.appender.RFA.MaxBackupIndex=${maxbackupindex}
+log4j.appender.RFA.layout=org.apache.log4j.PatternLayout
+log4j.appender.RFA.layout.ConversionPattern=%d{ISO8601} %p %c: %m%n
\ No newline at end of file
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 395da3b..317d950 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -116,6 +116,9 @@
         Improve PatternLayout performance by reducing unnecessary indirection and branching.
       </action>
       <!-- FIXES -->
+      <action issue="LOG4J2-2951" dev="rgoers">
+        Log4j 1.x properties were not being substituted.
+      </action>
       <action issue="LOG4J2-3201" dev="rgoers" type="fix">
         Limit the protocols JNDI can use by default. Limit the servers and classes that can be accessed via LDAP.
       </action>