Skip to content

Commit 28c405e

Browse files
Fix: Ensure jvmrunargs lookup is always registered (fixes #2726)
1 parent 286191e commit 28c405e

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.core.lookup;
18+
19+
import org.apache.logging.log4j.LogManager;
20+
import org.apache.logging.log4j.Logger;
21+
import org.junit.Test;
22+
23+
public class JvmRunArgsIntegrationTest {
24+
@Test
25+
public void testJvmRunArgsInConfig() {
26+
Logger logger = LogManager.getLogger(JvmRunArgsIntegrationTest.class);
27+
logger.info("Testing JVM Args lookup in config");
28+
}
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.core.lookup;
18+
19+
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertNull;
21+
22+
import org.apache.logging.log4j.core.LogEvent;
23+
import org.junit.Test;
24+
25+
public class JvmRunArgsLookupTest {
26+
27+
@Test
28+
public void testJvmRunArgsLookupIsRegistered() {
29+
Interpolator interpolator = new Interpolator();
30+
StrLookup lookup = interpolator.getStrLookupMap().get("jvmrunargs");
31+
assertNotNull("jvmrunargs lookup should be registered", lookup);
32+
}
33+
34+
@Test
35+
public void testJvmRunArgsLookupReturnsArgs() {
36+
Interpolator interpolator = new Interpolator();
37+
StrLookup lookup = interpolator.getStrLookupMap().get("jvmrunargs");
38+
// This will depend on the JVM args, but should not be null (may be empty)
39+
String anyArg = lookup.lookup((LogEvent) null, "-Xmx");
40+
// Accept null or a value, but the lookup should not throw
41+
// Most likely null unless -Xmx is set
42+
assertNull(anyArg);
43+
}
44+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration status="WARN">
19+
<Appenders>
20+
<Console name="Console" target="SYSTEM_OUT">
21+
<PatternLayout pattern="JVM Args: ${jvmrunargs:-Xmx}%n" />
22+
</Console>
23+
</Appenders>
24+
<Loggers>
25+
<Root level="info">
26+
<AppenderRef ref="Console"/>
27+
</Root>
28+
</Loggers>
29+
</Configuration>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,14 @@ 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+
}
96104
}
97105

98106
/**

0 commit comments

Comments
 (0)