Skip to content

Fix: Ensure jvmrunargs lookup is always registered (fixes #2726) #3874

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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");
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
29 changes: 29 additions & 0 deletions log4j-core-test/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="JVM Args: ${jvmrunargs:-Xmx}%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
38 changes: 38 additions & 0 deletions src/site/antora/modules/ROOT/pages/manual/lookups.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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`] – <<LowerLookup>> lower-cases its input
* {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

[[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:<index>}` syntax, where `<index>` is the zero-based
position of the JVM argument to retrieve.

For example:

[source,xml]
----
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="JVM Arg[0]: ${jvmrunargs:0} %m%n" />
</Console>
</Appenders>
----

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`.
Loading