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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]