You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "kalayciburak (via GitHub)" <gi...@apache.org> on 2026/09/25 11:46:50 UTC
[PR] Preserve Log4j 1 lookups when converting a properties configuration [logging-log4j2]
kalayciburak opened a new pull request, #4353:
URL: https://github.com/apache/logging-log4j2/pull/4353
`Log4j1ConfigurationConverter` shares `Log4j1ConfigurationParser` with the runtime factory. `getProperty` calls `OptionConverter.substVars`, which resolves each `${...}` against the current JVM before the value is written. A converted file therefore captures host system properties (`user.home`, paths, anything set with `-D`) and loses the indirection.
The converter now rewrites variables instead of resolving them:
- a name defined in the properties file stays `${name}`, and its value is emitted as a `<Property>` (nested system names inside that value become `${sys:...}`)
- any other name becomes `${sys:name}`
`Log4j1ConfigurationFactory` still uses the default parser and resolves variables.
Fixes #4348
## Checklist
* Base your changes on `2.x` branch if you are targeting Log4j 2; use `main` otherwise
* Non-trivial changes contain an entry file in the `src/changelog/.2.x.x` directory
* Tests are provided
`./mvnw verify` was not run.
## Tests
Executed with `JAVA_HOME` set to Java 17:
* `./mvnw -pl log4j-1.2-api -am -Dtest=Log4j1ConfigurationConverterLookupTest -Dsurefire.failIfNoSpecifiedTests=false test` — failed on current `2.x` because `fileName` was `/root/logs/app.log`, then passed after the fix (3 tests, 0 failures)
* `./mvnw -pl log4j-1.2-api -am -Dtest=Log4j1ConfigurationConverterLookupTest,Log4j1ConfigurationConverterHadoopTest,Log4j1ConfigurationConverterSparkTest,Log4j1ConfigurationFactoryTest -Dsurefire.failIfNoSpecifiedTests=false test` — 70 tests, 0 failures
* `./mvnw -pl log4j-1.2-api spotless:apply` — clean
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Preserve Log4j 1 lookups when converting a properties configuration [logging-log4j2]
Posted by "ramanathan1504 (via GitHub)" <gi...@apache.org>.
ramanathan1504 merged PR #4353:
URL: https://github.com/apache/logging-log4j2/pull/4353
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Preserve Log4j 1 lookups when converting a properties configuration [logging-log4j2]
Posted by "ramanathan1504 (via GitHub)" <gi...@apache.org>.
ramanathan1504 commented on code in PR #4353:
URL: https://github.com/apache/logging-log4j2/pull/4353#discussion_r4199502233
##########
src/changelog/.2.x.x/4348_log4j1_converter_preserve_lookups.xml:
##########
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns="https://logging.apache.org/xml/ns"
+ xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
+ type="fixed">
+ <issue id="4348" link="https://github.com/apache/logging-log4j2/issues/4348"/>
+ <issue id="4353" link="https://github.com/apache/logging-log4j2/pull/4353"/>
+ <description format="asciidoc">
+ Keep Log4j 1 variable indirection when converting a properties configuration, instead of capturing host system properties.
Review Comment:
```suggestion
Fix `Log4j1ConfigurationConverter` to write `${sys:name}` lookups instead of resolved system property values.
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Preserve Log4j 1 lookups when converting a properties configuration [logging-log4j2]
Posted by "kalayciburak (via GitHub)" <gi...@apache.org>.
kalayciburak commented on PR #4353:
URL: https://github.com/apache/logging-log4j2/pull/4353#issuecomment-5878438618
yes, every name is ${sys:name} now. dropped isDefinedProperty and added the threshold test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Preserve Log4j 1 lookups when converting a properties configuration [logging-log4j2]
Posted by "ramanathan1504 (via GitHub)" <gi...@apache.org>.
ramanathan1504 commented on code in PR #4353:
URL: https://github.com/apache/logging-log4j2/pull/4353#discussion_r4126615536
##########
log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java:
##########
@@ -452,9 +476,57 @@ private String getLog4jAppenderValue(final String appenderName, final String att
}
private String getProperty(final String key) {
- final String value = properties.getProperty(key);
- final String substVars = OptionConverter.substVars(value, properties);
- return substVars == null ? null : substVars.trim();
+ final String substituted = substitute(properties.getProperty(key));
+ return substituted == null ? null : substituted.trim();
+ }
+
+ private String substitute(final String value) {
+ if (value == null) {
+ return null;
+ }
+ return preserveLookups ? translateLookups(value) : OptionConverter.substVars(value, properties);
+ }
+
+ /**
+ * Rewrites Log4j 1 {@code ${name}} variables without reading the current JVM.
+ * A name defined in the properties file stays a configuration property. Any other name becomes {@code ${sys:name}}.
+ */
+ private String translateLookups(final String value) {
+ if (value == null) {
+ return null;
+ }
+ final StringBuilder translated = new StringBuilder();
+ int index = 0;
+ while (true) {
+ final int start = value.indexOf("${", index);
+ if (start < 0) {
+ if (index == 0) {
+ return value;
+ }
+ translated.append(value.substring(index));
+ return translated.toString();
+ }
+ final int end = value.indexOf('}', start);
+ if (end < 0) {
+ throw new IllegalArgumentException(
+ Strings.dquote(value) + " has no closing brace. Opening brace at position " + start + '.');
+ }
+ translated.append(value, index, start);
+ final String key = value.substring(start + 2, end);
+ if (isDefinedProperty(key)) {
+ translated.append("${").append(key).append('}');
+ } else {
+ translated.append("${sys:").append(key).append('}');
+ }
Review Comment:
Log4j 1 checks system properties before the file. `${sys:app.dir}` does the same and falls back to the `<Property>`, while `${app.dir}` ignores `-Dapp.dir`.
```suggestion
translated.append("${sys:").append(key).append('}');
```
##########
log4j-1.2-api/src/main/java/org/apache/log4j/config/Log4j1ConfigurationParser.java:
##########
@@ -452,9 +476,57 @@ private String getLog4jAppenderValue(final String appenderName, final String att
}
private String getProperty(final String key) {
- final String value = properties.getProperty(key);
- final String substVars = OptionConverter.substVars(value, properties);
- return substVars == null ? null : substVars.trim();
+ final String substituted = substitute(properties.getProperty(key));
+ return substituted == null ? null : substituted.trim();
+ }
+
+ private String substitute(final String value) {
+ if (value == null) {
+ return null;
+ }
+ return preserveLookups ? translateLookups(value) : OptionConverter.substVars(value, properties);
+ }
+
+ /**
+ * Rewrites Log4j 1 {@code ${name}} variables without reading the current JVM.
+ * A name defined in the properties file stays a configuration property. Any other name becomes {@code ${sys:name}}.
+ */
+ private String translateLookups(final String value) {
+ if (value == null) {
+ return null;
+ }
+ final StringBuilder translated = new StringBuilder();
+ int index = 0;
+ while (true) {
+ final int start = value.indexOf("${", index);
+ if (start < 0) {
+ if (index == 0) {
+ return value;
+ }
+ translated.append(value.substring(index));
+ return translated.toString();
+ }
+ final int end = value.indexOf('}', start);
+ if (end < 0) {
+ throw new IllegalArgumentException(
+ Strings.dquote(value) + " has no closing brace. Opening brace at position " + start + '.');
+ }
+ translated.append(value, index, start);
+ final String key = value.substring(start + 2, end);
+ if (isDefinedProperty(key)) {
+ translated.append("${").append(key).append('}');
+ } else {
+ translated.append("${sys:").append(key).append('}');
+ }
+ index = end + 1;
+ }
+ }
+
+ private boolean isDefinedProperty(final String key) {
+ return !key.startsWith("log4j.")
+ && !key.equals(ROOTCATEGORY)
+ && !key.equals(ROOTLOGGER)
+ && properties.getProperty(key) != null;
}
Review Comment:
With every name mapped to `${sys:...}`, this method has no caller.
```suggestion
```
##########
log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationConverterLookupTest.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The converter must keep Log4j 1 variable indirection. Resolving lookups while converting captures the host.
+ */
+public class Log4j1ConfigurationConverterLookupTest {
+
+ @Test
+ public void systemPropertyLookupIsNotResolved() throws Exception {
+ final String home = System.getProperty("user.home");
+ final String xml = convert("log4j.rootLogger=INFO, FILE\n"
+ + "log4j.appender.FILE=org.apache.log4j.FileAppender\n"
+ + "log4j.appender.FILE.File=${user.home}/logs/app.log\n");
+
+ assertFalse(xml.contains(home + "/logs/app.log"), xml);
+ assertTrue(xml.contains("${sys:user.home}/logs/app.log"), xml);
+ }
+
+ @Test
+ public void filePropertyKeepsItsNameAndTranslatesNestedSystemLookups() throws Exception {
+ final String home = System.getProperty("user.home");
+ final String xml = convert("log4j.rootLogger=INFO, FILE\n"
+ + "app.dir=${user.home}/data\n"
+ + "log4j.appender.FILE=org.apache.log4j.FileAppender\n"
+ + "log4j.appender.FILE.File=${app.dir}/app.log\n");
+
+ assertFalse(xml.contains(home), xml);
+ assertTrue(xml.contains("${app.dir}/app.log"), xml);
+ assertTrue(xml.contains("${sys:user.home}/data"), xml);
+ }
+
+ @Test
+ public void runtimeParserStillResolvesSystemProperties() throws Exception {
+ final String home = System.getProperty("user.home");
+ final String properties = "log4j.rootLogger=INFO, FILE\n"
+ + "log4j.appender.FILE=org.apache.log4j.FileAppender\n"
+ + "log4j.appender.FILE.File=${user.home}/logs/app.log\n";
+ final ByteArrayOutputStream output = new ByteArrayOutputStream();
+ new Log4j1ConfigurationParser()
+ .buildConfigurationBuilder(new ByteArrayInputStream(properties.getBytes(StandardCharsets.ISO_8859_1)))
+ .writeXmlConfiguration(output);
+ final String xml = output.toString("UTF-8");
+
+ assertTrue(xml.contains(home + "/logs/app.log"), xml);
+ assertFalse(xml.contains("${sys:user.home}"), xml);
+ }
Review Comment:
Nothing covers the threshold branch for a value with `${`. This test fails if that branch is removed.
```suggestion
}
@Test
public void thresholdLookupIsKept() throws Exception {
final String xml = convert("log4j.rootLogger=INFO, FILE\n"
+ "log4j.threshold=${lvl}\n"
+ "log4j.appender.FILE=org.apache.log4j.FileAppender\n"
+ "log4j.appender.FILE.File=app.log\n");
assertTrue(xml.contains("level=\"${sys:lvl}\""), xml);
}
```
##########
log4j-1.2-api/src/test/java/org/apache/log4j/config/Log4j1ConfigurationConverterLookupTest.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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 static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import org.junit.jupiter.api.Test;
+
+/**
+ * The converter must keep Log4j 1 variable indirection. Resolving lookups while converting captures the host.
+ */
+public class Log4j1ConfigurationConverterLookupTest {
+
+ @Test
+ public void systemPropertyLookupIsNotResolved() throws Exception {
+ final String home = System.getProperty("user.home");
+ final String xml = convert("log4j.rootLogger=INFO, FILE\n"
+ + "log4j.appender.FILE=org.apache.log4j.FileAppender\n"
+ + "log4j.appender.FILE.File=${user.home}/logs/app.log\n");
+
+ assertFalse(xml.contains(home + "/logs/app.log"), xml);
+ assertTrue(xml.contains("${sys:user.home}/logs/app.log"), xml);
+ }
+
+ @Test
+ public void filePropertyKeepsItsNameAndTranslatesNestedSystemLookups() throws Exception {
+ final String home = System.getProperty("user.home");
+ final String xml = convert("log4j.rootLogger=INFO, FILE\n"
+ + "app.dir=${user.home}/data\n"
+ + "log4j.appender.FILE=org.apache.log4j.FileAppender\n"
+ + "log4j.appender.FILE.File=${app.dir}/app.log\n");
+
+ assertFalse(xml.contains(home), xml);
+ assertTrue(xml.contains("${app.dir}/app.log"), xml);
Review Comment:
This assert fails after the two changes above. The file property now reads through `${sys:app.dir}`.
```suggestion
assertTrue(xml.contains("${sys:app.dir}/app.log"), xml);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: notifications-unsubscribe@logging.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org