You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2016/09/22 09:21:01 UTC

logging-log4j2 git commit: [LOG4J2-1523] Log4j 1 appenders. Support System properties per Log4j 1 Javadoc.

Repository: logging-log4j2
Updated Branches:
  refs/heads/master e2536923d -> 35eda8aa4


[LOG4J2-1523] Log4j 1 appenders. Support System properties per Log4j 1
Javadoc.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/35eda8aa
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/35eda8aa
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/35eda8aa

Branch: refs/heads/master
Commit: 35eda8aa4a569d26979b7ffe2cf11134963cc6c6
Parents: e253692
Author: Gary Gregory <gg...@apache.org>
Authored: Thu Sep 22 02:20:58 2016 -0700
Committer: Gary Gregory <gg...@apache.org>
Committed: Thu Sep 22 02:20:58 2016 -0700

----------------------------------------------------------------------
 .../log4j/config/Log4j1ConfigurationParser.java | 28 +++++++++++++++++---
 .../config/Log4j1ConfigurationFactoryTest.java  | 16 ++++++++++-
 .../log4j-system-properties-1.properties        | 14 ++++++++++
 .../log4j-system-properties-2.properties        | 15 +++++++++++
 4 files changed, 68 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/35eda8aa/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java
----------------------------------------------------------------------
diff --git a/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java b/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java
index 27c7807..16c9b7c 100644
--- a/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java
+++ b/log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java
@@ -43,6 +43,21 @@ import org.apache.logging.log4j.status.StatusLogger;
  * Experimental parser for Log4j 1.2 properties configuration files.
  *
  * This class is not thread-safe.
+ * 
+ * <p>
+ * From the Log4j 1.2 Javadocs:
+ * </p>
+ * <p>
+ * All option values admit variable substitution. The syntax of variable
+ * substitution is similar to that of Unix shells. The string between an opening
+ * "${" and closing "}" is interpreted as a key. The value of the substituted
+ * variable can be defined as a system property or in the configuration file
+ * itself. The value of the key is first searched in the system properties, and
+ * if not found there, it is then searched in the configuration file being
+ * parsed. The corresponding value replaces the ${variableName} sequence. For
+ * example, if java.home system property is set to /home/xyz, then every
+ * occurrence of the sequence ${java.home} will be interpreted as /home/xyz.
+ * </p>
  */
 public class Log4j1ConfigurationParser {
 
@@ -52,7 +67,8 @@ public class Log4j1ConfigurationParser {
 	private static final String FALSE = "false";
 	
 	private final Properties properties = new Properties();
-	private StrSubstitutor strSubstitutor;
+	private StrSubstitutor strSubstitutorProperties;
+	private StrSubstitutor strSubstitutorSystem;
 	
 	private final ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory
 			.newConfigurationBuilder();
@@ -72,7 +88,8 @@ public class Log4j1ConfigurationParser {
 	public ConfigurationBuilder<BuiltConfiguration> buildConfigurationBuilder(final InputStream input)
 			throws IOException {
 		properties.load(input);
-		strSubstitutor = new StrSubstitutor(properties);
+		strSubstitutorProperties = new StrSubstitutor(properties);
+		strSubstitutorSystem = new StrSubstitutor(System.getProperties());
 		final String rootCategoryValue = getLog4jValue(ROOTCATEGORY);
 		final String rootLoggerValue = getLog4jValue(ROOTLOGGER);
 		if (rootCategoryValue == null && rootLoggerValue == null) {
@@ -364,11 +381,14 @@ public class Log4j1ConfigurationParser {
 	}
 
 	private String getProperty(final String key) {
-		return strSubstitutor.replace(properties.getProperty(key));
+		final String value = properties.getProperty(key);
+		final String sysValue = strSubstitutorSystem.replace(value);
+		return strSubstitutorProperties.replace(sysValue);
 	}
 
 	private String getProperty(final String key, String defaultValue) {
-		return strSubstitutor.replace(properties.getProperty(key, defaultValue));
+		final String value = getProperty(key);
+		return value == null ? defaultValue : value;
 	}
 
 	private String getLog4jAppenderValue(final String appenderName, final String attributeName,

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/35eda8aa/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
----------------------------------------------------------------------
diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
index 1e5ff36..cb58dbe 100644
--- a/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationFactoryTest.java
@@ -77,7 +77,7 @@ public class Log4j1ConfigurationFactoryTest {
 
 	private Configuration configure(final String configResource) throws URISyntaxException {
 		final URL configLocation = ClassLoader.getSystemResource(configResource);
-		assertNotNull(configLocation);
+		assertNotNull(configResource, configLocation);
 		final Configuration configuration = new Log4j1ConfigurationFactory().getConfiguration(null, "test",
 				configLocation.toURI());
 		assertNotNull(configuration);
@@ -153,6 +153,20 @@ public class Log4j1ConfigurationFactoryTest {
 		testRollingFileAppender("config-1.2/log4j-RollingFileAppender-with-props.properties", "RFA", "./hadoop.log.%i");
 	}
 
+	@Test
+	public void testSystemProperties1() throws Exception {
+		final Configuration configuration = configure("config-1.2/log4j-system-properties-1.properties");
+		final RollingFileAppender appender = configuration.getAppender("RFA");
+		assertEquals(System.getProperty("java.io.tmpdir") + "/hadoop.log", appender.getFileName());
+	}
+
+	@Test
+	public void testSystemProperties2() throws Exception {
+		final Configuration configuration = configure("config-1.2/log4j-system-properties-2.properties");
+		final RollingFileAppender appender = configuration.getAppender("RFA");
+		assertEquals("${java.io.tmpdir}/hadoop.log", appender.getFileName());
+	}
+
 	private void testRollingFileAppender(final String configResource, final String name, final String filePattern) throws URISyntaxException {
 		final Configuration configuration = configure(configResource);
 		final Appender appender = configuration.getAppender(name);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/35eda8aa/log4j-1.2-api/src/test/resources/config-1.2/log4j-system-properties-1.properties
----------------------------------------------------------------------
diff --git a/log4j-1.2-api/src/test/resources/config-1.2/log4j-system-properties-1.properties b/log4j-1.2-api/src/test/resources/config-1.2/log4j-system-properties-1.properties
new file mode 100644
index 0000000..a82c4c3
--- /dev/null
+++ b/log4j-1.2-api/src/test/resources/config-1.2/log4j-system-properties-1.properties
@@ -0,0 +1,14 @@
+###############################################################################
+#
+# Log4J 1.2 Configuration.
+#
+
+hadoop.log.file=hadoop.log
+
+log4j.rootLogger=TRACE, RFA
+
+#
+# Rolling File Appender
+#
+log4j.appender.RFA=org.apache.log4j.RollingFileAppender
+log4j.appender.RFA.File=${java.io.tmpdir}/${hadoop.log.file}

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/35eda8aa/log4j-1.2-api/src/test/resources/config-1.2/log4j-system-properties-2.properties
----------------------------------------------------------------------
diff --git a/log4j-1.2-api/src/test/resources/config-1.2/log4j-system-properties-2.properties b/log4j-1.2-api/src/test/resources/config-1.2/log4j-system-properties-2.properties
new file mode 100644
index 0000000..9228434
--- /dev/null
+++ b/log4j-1.2-api/src/test/resources/config-1.2/log4j-system-properties-2.properties
@@ -0,0 +1,15 @@
+###############################################################################
+#
+# Log4J 1.2 Configuration.
+#
+
+hadoop.log.dir=${java.io.tmpdir}
+hadoop.log.file=hadoop.log
+
+log4j.rootLogger=TRACE, RFA
+
+#
+# Rolling File Appender
+#
+log4j.appender.RFA=org.apache.log4j.RollingFileAppender
+log4j.appender.RFA.File=${hadoop.log.dir}/${hadoop.log.file}