Skip to content

Commit 151f7a6

Browse files
committed
Revamp the Lookups page
We revamp the `Lookups` page by: * Adding information on different evaluation contexts supported by lookups. * Adding a cheatsheet on which lookup is available in which evaluation context. E.g., using `$${sys:something}` on a per-event level is a major performance waster. * Removing the description of the broken `jvmrunargs` lookup (see #2726). * Improving the description of the `main` lookup by providing a code example that does not fail if the user switches logging implementations. * Shortening and improving the description of other lookups.
1 parent 3cc1625 commit 151f7a6

File tree

24 files changed

+1127
-557
lines changed

24 files changed

+1127
-557
lines changed

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

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.junit.jupiter.api.Assertions.assertNull;
2020

21+
import java.util.Map;
2122
import org.junit.jupiter.api.Test;
2223

2324
/**
@@ -36,15 +37,40 @@ public static void main(final String[] args) {
3637
@Test
3738
public void testMap() {
3839
final JmxRuntimeInputArgumentsLookup lookup = JmxRuntimeInputArgumentsLookup.JMX_SINGLETON;
39-
assertNull(lookup.lookup(null));
40-
assertNull(lookup.lookup("X"));
41-
assertNull(lookup.lookup("foo.txt"));
40+
String result2 = null;
41+
if (null != null) {
42+
final Map<String, String> map2 = lookup.getMap();
43+
result2 = map2 == null ? null : map2.get(null);
44+
}
45+
assertNull(result2);
46+
String result1 = null;
47+
if ("X" != null) {
48+
final Map<String, String> map1 = lookup.getMap();
49+
result1 = map1 == null ? null : map1.get("X");
50+
}
51+
assertNull(result1);
52+
String result = null;
53+
if ("foo.txt" != null) {
54+
final Map<String, String> map = lookup.getMap();
55+
result = map == null ? null : map.get("foo.txt");
56+
}
57+
assertNull(result);
4258
}
4359

4460
public void callFromMain() {
4561
final JmxRuntimeInputArgumentsLookup lookup = JmxRuntimeInputArgumentsLookup.JMX_SINGLETON;
46-
assertNull(lookup.lookup(null));
47-
assertNull(lookup.lookup("X"));
62+
String result1 = null;
63+
if (null != null) {
64+
final Map<String, String> map1 = lookup.getMap();
65+
result1 = map1 == null ? null : map1.get(null);
66+
}
67+
assertNull(result1);
68+
String result = null;
69+
if ("X" != null) {
70+
final Map<String, String> map = lookup.getMap();
71+
result = map == null ? null : map.get("X");
72+
}
73+
assertNull(result);
4874
// Eclipse adds -Dfile.encoding=Cp1252
4975
// assertEquals("--file", lookup.lookup("0"));
5076
// assertEquals("foo.txt", lookup.lookup("1"));

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

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

19+
import org.apache.logging.log4j.core.LogEvent;
20+
1921
/**
2022
* A default lookup for others to extend.
2123
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public class EnvironmentLookup extends AbstractLookup {
2727

2828
/**
2929
* Looks up the value of the environment variable.
30-
* @param event The current LogEvent (is ignored by this StrLookup).
30+
*
3131
* @param key the key to be looked up, may be null
3232
* @return The value of the environment variable.
3333
*/
3434
@Override
35-
public String lookup(final LogEvent event, final String key) {
35+
public String lookup(final LogEvent ignored, final String key) {
3636
return System.getenv(key);
3737
}
3838
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,12 @@ public String getVirtualMachine() {
9090
/**
9191
* Looks up the value of the environment variable.
9292
*
93-
* @param event
94-
* The current LogEvent (is ignored by this StrLookup).
9593
* @param key
9694
* the key to be looked up, may be null
9795
* @return The value of the environment variable.
9896
*/
9997
@Override
100-
public String lookup(final LogEvent event, final String key) {
98+
public String lookup(final LogEvent ignored, final String key) {
10199
switch (key) {
102100
case "version":
103101
return "Java version " + getSystemProperty("java.version");

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,7 @@ public JmxRuntimeInputArgumentsLookup(final Map<String, String> map) {
4848
}
4949

5050
@Override
51-
public String lookup(final LogEvent event, final String key) {
52-
return lookup(key);
53-
}
54-
55-
@Override
56-
public String lookup(final String key) {
51+
public String lookup(final LogEvent ignored, final String key) {
5752
if (key == null) {
5853
return null;
5954
}

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,11 @@ public JndiLookup() {
5050
/**
5151
* Looks up the value of the JNDI resource.
5252
*
53-
* @param event The current LogEvent (is ignored by this StrLookup).
5453
* @param key the JNDI resource name to be looked up, may be null
5554
* @return The String value of the JNDI resource.
5655
*/
5756
@Override
58-
public String lookup(final LogEvent event, final String key) {
57+
public String lookup(final LogEvent ignored, final String key) {
5958
if (key == null) {
6059
return null;
6160
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private static URI getParent(final URI uri) throws URISyntaxException {
5353
}
5454

5555
@Override
56-
public String lookup(final LogEvent event, final String key) {
56+
public String lookup(final LogEvent ignored, final String key) {
5757
if (configuration != null) {
5858
final ConfigurationSource configSrc = configuration.getConfigurationSource();
5959
final File file = configSrc.getFile();

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

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,16 @@
2525
* Converts values to lower case. The passed in "key" should be the value of another lookup.
2626
*/
2727
@Plugin(name = "lower", category = StrLookup.CATEGORY)
28-
public class LowerLookup implements StrLookup {
28+
public class LowerLookup extends AbstractLookup {
2929

3030
/**
3131
* Converts the "key" to lower case.
32+
*
3233
* @param key the key to be looked up, may be null
3334
* @return The value associated with the key.
3435
*/
3536
@Override
36-
public String lookup(final String key) {
37+
public String lookup(final LogEvent ignored, final String key) {
3738
return key != null ? toRootLowerCase(key) : null;
3839
}
39-
40-
/**
41-
* Converts the "key" to lower case.
42-
* @param event The current LogEvent.
43-
* @param key the key to be looked up, may be null
44-
* @return The value associated with the key.
45-
*/
46-
@Override
47-
public String lookup(final LogEvent event, final String key) {
48-
return lookup(key);
49-
}
5040
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static void setMainArguments(final String... args) {
8282
}
8383

8484
@Override
85-
public String lookup(final LogEvent event, final String key) {
85+
public String lookup(final LogEvent ignored, final String key) {
8686
return MAIN_SINGLETON.getMap().get(key);
8787
}
8888

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,12 @@ public class ResourceBundleLookup extends AbstractLookup {
3939
*
4040
* For example: "com.domain.messages:MyKey".
4141
*
42-
* @param event
43-
* The current LogEvent.
4442
* @param key
4543
* the key to be looked up, may be null
4644
* @return The value associated with the key.
4745
*/
4846
@Override
49-
public String lookup(final LogEvent event, final String key) {
47+
public String lookup(final LogEvent ignored, final String key) {
5048
if (key == null) {
5149
return null;
5250
}

0 commit comments

Comments
 (0)