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 2022/02/06 17:38:57 UTC
[logging-log4j2] branch release-2.x updated: LOG4J2-3394 - Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341
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 6d6ff5b LOG4J2-3394 - Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341
6d6ff5b is described below
commit 6d6ff5b85bbdba8895c41de5c5b6049e07635395
Author: Ralph Goers <rg...@apache.org>
AuthorDate: Sun Feb 6 10:38:37 2022 -0700
LOG4J2-3394 - Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341
---
.../properties/PropertiesConfigurationBuilder.java | 8 ++++++--
.../logging/log4j/core/lookup/Interpolator.java | 14 +++++++++++++
.../properties/PropertiesConfigurationTest.java | 22 ++++++++++++++++++++
.../log4j/core/lookup/InterpolatorTest.java | 17 +++++++++++++++
.../LoggerLevelAppenderTestWithSubst.properties | 24 ++++++++++++++++++++++
src/changes/changes.xml | 3 +++
6 files changed, 86 insertions(+), 2 deletions(-)
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationBuilder.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationBuilder.java
index bc7c0cd..122f88c 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationBuilder.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationBuilder.java
@@ -38,6 +38,8 @@ import org.apache.logging.log4j.core.config.builder.api.RootLoggerComponentBuild
import org.apache.logging.log4j.core.config.builder.api.ScriptComponentBuilder;
import org.apache.logging.log4j.core.config.builder.api.ScriptFileComponentBuilder;
import org.apache.logging.log4j.core.filter.AbstractFilter.AbstractFilterBuilder;
+import org.apache.logging.log4j.core.lookup.Interpolator;
+import org.apache.logging.log4j.core.lookup.StrSubstitutor;
import org.apache.logging.log4j.core.util.Builder;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.PropertiesUtil;
@@ -69,6 +71,7 @@ public class PropertiesConfigurationBuilder extends ConfigurationBuilderFactory
private static final String CONFIG_TYPE = "type";
private final ConfigurationBuilder<PropertiesConfiguration> builder;
+ private final StrSubstitutor subst = new StrSubstitutor(new Interpolator((Properties) null));
private LoggerContext loggerContext;
private Properties rootProperties;
@@ -197,9 +200,10 @@ public class PropertiesConfigurationBuilder extends ConfigurationBuilderFactory
return builder.build(false);
}
- private static void useSyntheticLevelAndAppenderRefs(final String propertyName, final Properties loggerProps, final Properties context) {
- final String propertyValue = (String) context.remove(propertyName);
+ private void useSyntheticLevelAndAppenderRefs(final String propertyName, final Properties loggerProps, final Properties context) {
+ String propertyValue = (String) context.remove(propertyName);
if (propertyValue != null) {
+ propertyValue = subst.replace(propertyValue);
final String[] parts = propertyValue.split(",");
if (parts.length > 0) {
final String level = parts[0].trim();
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
index 86a742e..76a6a6c 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java
@@ -21,6 +21,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
+import java.util.Properties;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent;
@@ -94,6 +95,19 @@ public class Interpolator extends AbstractConfigurationAwareLookup {
}
/**
+ * Create a limited Interpolator.
+ */
+ public Interpolator(final Properties properties) {
+ Map<String, String> defaultProps = new HashMap<>();
+ if (properties != null) {
+ properties.forEach((key, value) -> defaultProps.put(key.toString(), value.toString()));
+ }
+ this.defaultLookup = new PropertiesLookup(defaultProps);
+ strLookupMap.put("sys", new SystemPropertiesLookup());
+ strLookupMap.put("env", new EnvironmentLookup());
+ }
+
+ /**
* Creates the default Interpolator with the provided properties.
*/
public Interpolator(final Map<String, String> properties) {
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationTest.java
index ec6fcd5..5174283 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/properties/PropertiesConfigurationTest.java
@@ -31,6 +31,7 @@ import org.apache.logging.log4j.junit.LoggerContextSource;
import org.apache.logging.log4j.junit.Named;
import org.apache.logging.log4j.test.appender.ListAppender;
import org.junit.jupiter.api.Test;
+import org.junitpioneer.jupiter.SetSystemProperty;
import java.util.List;
import java.util.Map;
@@ -130,4 +131,25 @@ class PropertiesConfigurationTest {
assertEquals(firstEvents, secondEvents);
assertEquals(1, firstEvents.size());
}
+
+ @Test
+ @LoggerContextSource("LoggerLevelAppenderTestWithSubst.properties")
+ void testLoggerLevelAppenderWithSubst(final LoggerContext context, @Named final ListAppender first, @Named final ListAppender second) {
+ context.getLogger(getClass()).atInfo().log("message");
+ final List<LogEvent> firstEvents = first.getEvents();
+ final List<LogEvent> secondEvents = second.getEvents();
+ assertEquals(firstEvents, secondEvents);
+ assertEquals(1, firstEvents.size());
+ }
+
+ @SetSystemProperty(key = "core.config.logger", value = "DEBUG , second")
+ @Test
+ @LoggerContextSource("LoggerLevelAppenderTestWithSubst.properties")
+ void testLoggerLevelAppenderWithSysSubst(final LoggerContext context, @Named final ListAppender first, @Named final ListAppender second) {
+ context.getLogger(getClass()).atInfo().log("message");
+ final List<LogEvent> firstEvents = first.getEvents();
+ final List<LogEvent> secondEvents = second.getEvents();
+ assertEquals(0, firstEvents.size());
+ assertEquals(1, secondEvents.size());
+ }
}
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java
index 456a744..4294051 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/lookup/InterpolatorTest.java
@@ -21,6 +21,7 @@ import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
+import java.util.Properties;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.ThreadContext;
@@ -46,6 +47,10 @@ public class InterpolatorTest {
private static final String TESTKEY = "TestKey";
private static final String TESTKEY2 = "TestKey2";
private static final String TESTVAL = "TestValue";
+ private static final String TESTKEY3 = "TestKey3";
+ private static final String TESTKEY4 = "TestKey4";
+ private static final String TESTVAL3 = "${sys:TestKey4:-default}";
+ private static final String TESTVAL4 = "TestVal4";
private static final String TEST_CONTEXT_RESOURCE_NAME = "logging/context-name";
private static final String TEST_CONTEXT_NAME = "app-1";
@@ -56,6 +61,8 @@ public class InterpolatorTest {
protected void before() throws Throwable {
System.setProperty(TESTKEY, TESTVAL);
System.setProperty(TESTKEY2, TESTVAL);
+ System.setProperty(TESTKEY3, TESTVAL3);
+ System.setProperty(TESTKEY4, TESTVAL4);
System.setProperty("log4j2.enableJndiLookup", "true");
}
@@ -63,6 +70,8 @@ public class InterpolatorTest {
protected void after() {
System.clearProperty(TESTKEY);
System.clearProperty(TESTKEY2);
+ System.clearProperty(TESTKEY3);
+ System.clearProperty(TESTKEY4);
System.clearProperty("log4j2.enableJndiLookup");
}
}).around(new JndiRule(
@@ -177,4 +186,12 @@ public class InterpolatorTest {
.build();
assertEquals("mapMessage", interpolator.lookup(event, "map:key"));
}
+
+ @Test
+ public void testLimited() {
+ Interpolator interpolator = new Interpolator((Properties) null);
+ String original = "${sys:TestKey3}";
+ String result = new StrSubstitutor(interpolator).replace(original);
+ assertEquals(TESTVAL3, result);
+ }
}
diff --git a/log4j-core/src/test/resources/LoggerLevelAppenderTestWithSubst.properties b/log4j-core/src/test/resources/LoggerLevelAppenderTestWithSubst.properties
new file mode 100644
index 0000000..868e809
--- /dev/null
+++ b/log4j-core/src/test/resources/LoggerLevelAppenderTestWithSubst.properties
@@ -0,0 +1,24 @@
+#
+# 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.
+#
+
+status = off
+name = LoggerLevelAppenderTest
+logger.core-config-properties.name = org.apache.logging.log4j.core.config.properties
+# whitespace added for testing trimming
+logger.core-config-properties = ${sys:core.config.logger:-INFO , first , second}
+appender.first.type = List
+appender.second.type = List
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index c59773a..f88ca08 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -209,6 +209,9 @@
AppenderLoggingException logging any exception to a MongoDB Appender.
</action>
<!-- ADD -->
+ <action issue="LOG4J2-3394" dev="rgoers" type="fix">
+ Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341.
+ </action>
<action issue="LOG4J2-2486" dev="rgoers" type="add">
Require log4j2.Script.enableLanguages to be specified to enable scripting for specific languages.
</action>
Re: [logging-log4j2] branch release-2.x updated: LOG4J2-3394 - Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341
Posted by Ralph Goers <ra...@dslextreme.com>.
See my reply. If you have an alternate way to implement this feel free. But trying to do it during the course of “normal” properties processing seems like it would be very messy.
Ralph
> On Feb 6, 2022, at 1:55 PM, Carter Kozak <ck...@ckozak.net> wrote:
>
> I have added a few review comments on this change
>
> https://github.com/apache/logging-log4j2/commit/b69b7b802539d87aab6b51aca0a0df8a669ce6ee
>
> -ck
>
> On Sun, Feb 6, 2022, at 12:38, rgoers@apache.org wrote:
>> 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 6d6ff5b LOG4J2-3394 - Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341
>> 6d6ff5b is described below
>>
>> commit 6d6ff5b85bbdba8895c41de5c5b6049e07635395
>> Author: Ralph Goers <rg...@apache.org>
>> AuthorDate: Sun Feb 6 10:38:37 2022 -0700
>>
>> LOG4J2-3394 - Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341
Re: [logging-log4j2] branch release-2.x updated: LOG4J2-3394 - Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341
Posted by Carter Kozak <ck...@ckozak.net>.
I have added a few review comments on this change
https://github.com/apache/logging-log4j2/commit/b69b7b802539d87aab6b51aca0a0df8a669ce6ee
-ck
On Sun, Feb 6, 2022, at 12:38, rgoers@apache.org wrote:
> 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 6d6ff5b LOG4J2-3394 - Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341
> 6d6ff5b is described below
>
> commit 6d6ff5b85bbdba8895c41de5c5b6049e07635395
> Author: Ralph Goers <rg...@apache.org>
> AuthorDate: Sun Feb 6 10:38:37 2022 -0700
>
> LOG4J2-3394 - Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341
Re: [logging-log4j2] branch release-2.x updated: LOG4J2-3394 - Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341
Posted by Carter Kozak <ck...@ckozak.net>.
-1 see GitHub comments regarding recursive lookup evaluation
https://github.com/apache/logging-log4j2/commit/6d6ff5b85bbdba8895c41de5c5b6049e07635395
-ck
> On Feb 6, 2022, at 12:39, rgoers@apache.org wrote:
> 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 6d6ff5b LOG4J2-3394 - Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341
> 6d6ff5b is described below
>
> commit 6d6ff5b85bbdba8895c41de5c5b6049e07635395
> Author: Ralph Goers <rg...@apache.org>
> AuthorDate: Sun Feb 6 10:38:37 2022 -0700
>
> LOG4J2-3394 - Allow substitution of system properties and environment variables in shorthand variables introduced in LOG4J2-3341