diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/JvmRunArgsIntegrationTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/JvmRunArgsIntegrationTest.java new file mode 100644 index 00000000000..5819e0f783c --- /dev/null +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/JvmRunArgsIntegrationTest.java @@ -0,0 +1,29 @@ +/* + * 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.logging.log4j.core.lookup; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.Test; + +public class JvmRunArgsIntegrationTest { + @Test + public void testJvmRunArgsInConfig() { + Logger logger = LogManager.getLogger(JvmRunArgsIntegrationTest.class); + logger.info("Testing JVM Args lookup in config"); + } +} \ No newline at end of file diff --git a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/JvmRunArgsLookupTest.java b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/JvmRunArgsLookupTest.java new file mode 100644 index 00000000000..40de8eeb4d5 --- /dev/null +++ b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/lookup/JvmRunArgsLookupTest.java @@ -0,0 +1,44 @@ +/* + * 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.logging.log4j.core.lookup; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + +import org.apache.logging.log4j.core.LogEvent; +import org.junit.jupiter.api.Test; + +public class JvmRunArgsLookupTest { + + @Test + public void testJvmRunArgsLookupIsRegistered() { + Interpolator interpolator = new Interpolator(); + StrLookup lookup = interpolator.getStrLookupMap().get("jvmrunargs"); + assertNotNull(lookup, "jvmrunargs lookup should be registered"); + } + + @Test + public void testJvmRunArgsLookupReturnsArgs() { + Interpolator interpolator = new Interpolator(); + StrLookup lookup = interpolator.getStrLookupMap().get("jvmrunargs"); + // This will depend on the JVM args, but should not be null (may be empty) + String anyArg = lookup.lookup((LogEvent) null, "-Xmx"); + // Accept null or a value, but the lookup should not throw + // Most likely null unless -Xmx is set + assertNull(anyArg); + } +} \ No newline at end of file diff --git a/log4j-core-test/src/test/resources/log4j2-test.xml b/log4j-core-test/src/test/resources/log4j2-test.xml new file mode 100644 index 00000000000..fe6cab5efb9 --- /dev/null +++ b/log4j-core-test/src/test/resources/log4j2-test.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + diff --git a/src/site/antora/modules/ROOT/pages/manual/lookups.adoc b/src/site/antora/modules/ROOT/pages/manual/lookups.adoc index b6281772bb6..f26dd894438 100644 --- a/src/site/antora/modules/ROOT/pages/manual/lookups.adoc +++ b/src/site/antora/modules/ROOT/pages/manual/lookups.adoc @@ -1052,3 +1052,41 @@ You can check out the following files for examples: * {project-github-url}/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/LowerLookup.java[`LowerLookup.java`] – <> lower-cases its input * {project-github-url}/log4j-core/src/main/java/org/apache/logging/log4j/core/lookup/EventLookup.java[`EventLookup.java`] – <> extracts specified fields from the effective `LogEvent` in the context + +[[JVMRunArgsLookup]] +== JVM Runtime Arguments Lookup + +The `jvmrunargs` lookup allows retrieval of the JVM runtime arguments +passed to the Java process at startup. + +=== Usage + +Use the `${jvmrunargs:}` syntax, where `` is the zero-based +position of the JVM argument to retrieve. + +For example: + +[source,xml] +---- + + + + + +---- + +If the specified index is out of range, the lookup returns `null`. + +=== Example Output + +Given the following JVM startup arguments: + +The lookup expressions would produce: + +- `${jvmrunargs:0}` → `-DexampleArg=value` +- `${jvmrunargs:1}` → `-Xmx512m` + +=== Notes + +* This lookup was fixed in version 2.25.0. +* If no JVM arguments are available, all lookups will return `null`.