Skip to content

Commit de8c6c9

Browse files
Add documentation for jvmrunargs lookup and verify functionality in 2.25.0
- Documented `jvmrunargs` lookup in `lookups.adoc` with usage examples. - Verified that the lookup works correctly in Log4j 2.25.0 using sample app and JVM args. - Included example configuration demonstrating retrieval of JVM runtime arguments. Closes apache#2726
1 parent 65ab704 commit de8c6c9

File tree

4 files changed

+45
-15
lines changed

4 files changed

+45
-15
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/JvmRunArgsIntegrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@
1818

1919
import org.apache.logging.log4j.LogManager;
2020
import org.apache.logging.log4j.Logger;
21-
import org.junit.Test;
21+
import org.junit.jupiter.api.Test;
2222

2323
public class JvmRunArgsIntegrationTest {
2424
@Test
2525
public void testJvmRunArgsInConfig() {
2626
Logger logger = LogManager.getLogger(JvmRunArgsIntegrationTest.class);
2727
logger.info("Testing JVM Args lookup in config");
2828
}
29-
}
29+
}

log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/JvmRunArgsLookupTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@
1616
*/
1717
package org.apache.logging.log4j.core.lookup;
1818

19-
import static org.junit.Assert.assertNotNull;
20-
import static org.junit.Assert.assertNull;
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
import static org.junit.jupiter.api.Assertions.assertNull;
2121

2222
import org.apache.logging.log4j.core.LogEvent;
23-
import org.junit.Test;
23+
import org.junit.jupiter.api.Test;
2424

2525
public class JvmRunArgsLookupTest {
2626

2727
@Test
2828
public void testJvmRunArgsLookupIsRegistered() {
2929
Interpolator interpolator = new Interpolator();
3030
StrLookup lookup = interpolator.getStrLookupMap().get("jvmrunargs");
31-
assertNotNull("jvmrunargs lookup should be registered", lookup);
31+
assertNotNull(lookup, "jvmrunargs lookup should be registered");
3232
}
3333

3434
@Test
@@ -41,4 +41,4 @@ public void testJvmRunArgsLookupReturnsArgs() {
4141
// Most likely null unless -Xmx is set
4242
assertNull(anyArg);
4343
}
44-
}
44+
}

log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/Interpolator.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,6 @@ public Interpolator(final StrLookup defaultLookup, final List<String> pluginPack
9393
handleError(entry.getKey(), t);
9494
}
9595
}
96-
97-
if (!strLookupMap.containsKey(LOOKUP_KEY_JVMRUNARGS)) {
98-
try {
99-
strLookupMap.put(LOOKUP_KEY_JVMRUNARGS, new JmxRuntimeInputArgumentsLookup());
100-
} catch (Throwable t) {
101-
handleError(LOOKUP_KEY_JVMRUNARGS, t);
102-
}
103-
}
10496
}
10597

10698
/**

src/site/antora/modules/ROOT/pages/manual/lookups.adoc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,3 +1052,41 @@ You can check out the following files for examples:
10521052
10531053
* {project-github-url}/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/LowerLookup.java[`LowerLookup.java`] – <<LowerLookup>> lower-cases its input
10541054
* {project-github-url}/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/EventLookup.java[`EventLookup.java`] – <<EventLookup>> extracts specified fields from the effective `LogEvent` in the context
1055+
1056+
[[JVMRunArgsLookup]]
1057+
== JVM Runtime Arguments Lookup
1058+
1059+
The `jvmrunargs` lookup allows retrieval of the JVM runtime arguments
1060+
passed to the Java process at startup.
1061+
1062+
=== Usage
1063+
1064+
Use the `${jvmrunargs:<index>}` syntax, where `<index>` is the zero-based
1065+
position of the JVM argument to retrieve.
1066+
1067+
For example:
1068+
1069+
[source,xml]
1070+
----
1071+
<Appenders>
1072+
<Console name="Console" target="SYSTEM_OUT">
1073+
<PatternLayout pattern="JVM Arg[0]: ${jvmrunargs:0} %m%n" />
1074+
</Console>
1075+
</Appenders>
1076+
----
1077+
1078+
If the specified index is out of range, the lookup returns `null`.
1079+
1080+
=== Example Output
1081+
1082+
Given the following JVM startup arguments:
1083+
1084+
The lookup expressions would produce:
1085+
1086+
- `${jvmrunargs:0}` → `-DexampleArg=value`
1087+
- `${jvmrunargs:1}` → `-Xmx512m`
1088+
1089+
=== Notes
1090+
1091+
* This lookup was fixed in version 2.25.0.
1092+
* If no JVM arguments are available, all lookups will return `null`.

0 commit comments

Comments
 (0)