/
Interpolator.java
258 lines (232 loc) · 10.8 KB
/
Interpolator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/*
* 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 java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.ConfigurationAware;
import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
import org.apache.logging.log4j.core.config.plugins.util.PluginType;
import org.apache.logging.log4j.core.net.JndiManager;
import org.apache.logging.log4j.core.util.Loader;
import org.apache.logging.log4j.core.util.ReflectionUtil;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.Constants;
/**
* Proxies all the other {@link StrLookup}s.
*/
public class Interpolator extends AbstractConfigurationAwareLookup {
/** Constant for the prefix separator. */
public static final char PREFIX_SEPARATOR = ':';
private static final String LOOKUP_KEY_WEB = "web";
private static final String LOOKUP_KEY_DOCKER = "docker";
private static final String LOOKUP_KEY_KUBERNETES = "kubernetes";
private static final String LOOKUP_KEY_SPRING = "spring";
private static final String LOOKUP_KEY_JNDI = "jndi";
private static final String LOOKUP_KEY_JVMRUNARGS = "jvmrunargs";
private static final Logger LOGGER = StatusLogger.getLogger();
private final Map<String, StrLookup> strLookupMap = new HashMap<>();
private final StrLookup defaultLookup;
public Interpolator(final StrLookup defaultLookup) {
this(defaultLookup, null);
}
/**
* Constructs an Interpolator using a given StrLookup and a list of packages to find Lookup plugins in.
*
* @param defaultLookup the default StrLookup to use as a fallback
* @param pluginPackages a list of packages to scan for Lookup plugins
* @since 2.1
*/
public Interpolator(final StrLookup defaultLookup, final List<String> pluginPackages) {
this.defaultLookup = defaultLookup == null ? new MapLookup(new HashMap<String, String>()) : defaultLookup;
final PluginManager manager = new PluginManager(CATEGORY);
manager.collectPlugins(pluginPackages);
final Map<String, PluginType<?>> plugins = manager.getPlugins();
for (final Map.Entry<String, PluginType<?>> entry : plugins.entrySet()) {
try {
final Class<? extends StrLookup> clazz = entry.getValue().getPluginClass().asSubclass(StrLookup.class);
if (!clazz.getName().equals(JndiLookup.class.getName()) || JndiManager.isIsJndiEnabled()) {
strLookupMap.put(entry.getKey().toLowerCase(), ReflectionUtil.instantiate(clazz));
}
} catch (final Throwable t) {
handleError(entry.getKey(), t);
}
}
}
/**
* Create the default Interpolator using only Lookups that work without an event.
*/
public Interpolator() {
this((Map<String, String>) null);
}
/**
* Creates the Interpolator using only Lookups that work without an event and initial properties.
*/
public Interpolator(final Map<String, String> properties) {
this.defaultLookup = new MapLookup(properties == null ? new HashMap<String, String>() : properties);
// TODO: this ought to use the PluginManager
strLookupMap.put("log4j", new Log4jLookup());
strLookupMap.put("sys", new SystemPropertiesLookup());
strLookupMap.put("env", new EnvironmentLookup());
strLookupMap.put("main", MainMapLookup.MAIN_SINGLETON);
strLookupMap.put("marker", new MarkerLookup());
strLookupMap.put("java", new JavaLookup());
strLookupMap.put("lower", new LowerLookup());
strLookupMap.put("upper", new UpperLookup());
// JNDI
if (JndiManager.isIsJndiEnabled()) {
try {
// [LOG4J2-703] We might be on Android
strLookupMap.put(LOOKUP_KEY_JNDI,
Loader.newCheckedInstanceOf("org.apache.logging.log4j.core.lookup.JndiLookup", StrLookup.class));
} catch (final LinkageError | Exception e) {
handleError(LOOKUP_KEY_JNDI, e);
}
}
// JMX input args
try {
// We might be on Android
strLookupMap.put(LOOKUP_KEY_JVMRUNARGS,
Loader.newCheckedInstanceOf("org.apache.logging.log4j.core.lookup.JmxRuntimeInputArgumentsLookup",
StrLookup.class));
} catch (final LinkageError | Exception e) {
handleError(LOOKUP_KEY_JVMRUNARGS, e);
}
strLookupMap.put("date", new DateLookup());
strLookupMap.put("ctx", new ContextMapLookup());
if (Constants.IS_WEB_APP) {
try {
strLookupMap.put(LOOKUP_KEY_WEB,
Loader.newCheckedInstanceOf("org.apache.logging.log4j.web.WebLookup", StrLookup.class));
} catch (final Exception ignored) {
handleError(LOOKUP_KEY_WEB, ignored);
}
} else {
LOGGER.debug("Not in a ServletContext environment, thus not loading WebLookup plugin.");
}
try {
strLookupMap.put(LOOKUP_KEY_DOCKER,
Loader.newCheckedInstanceOf("org.apache.logging.log4j.docker.DockerLookup", StrLookup.class));
} catch (final Exception ignored) {
handleError(LOOKUP_KEY_DOCKER, ignored);
}
try {
strLookupMap.put(LOOKUP_KEY_SPRING,
Loader.newCheckedInstanceOf("org.apache.logging.log4j.spring.cloud.config.client.SpringLookup", StrLookup.class));
} catch (final Exception ignored) {
handleError(LOOKUP_KEY_SPRING, ignored);
}
try {
strLookupMap.put(LOOKUP_KEY_KUBERNETES,
Loader.newCheckedInstanceOf("org.apache.logging.log4j.kubernetes.KubernetesLookup", StrLookup.class));
} catch (final Exception | NoClassDefFoundError error) {
handleError(LOOKUP_KEY_KUBERNETES, error);
}
}
public Map<String, StrLookup> getStrLookupMap() {
return strLookupMap;
}
private void handleError(final String lookupKey, final Throwable t) {
switch (lookupKey) {
case LOOKUP_KEY_JNDI:
// java.lang.VerifyError: org/apache/logging/log4j/core/lookup/JndiLookup
LOGGER.warn( // LOG4J2-1582 don't print the whole stack trace (it is just a warning...)
"JNDI lookup class is not available because this JRE does not support JNDI." +
" JNDI string lookups will not be available, continuing configuration. Ignoring " + t);
break;
case LOOKUP_KEY_JVMRUNARGS:
// java.lang.VerifyError: org/apache/logging/log4j/core/lookup/JmxRuntimeInputArgumentsLookup
LOGGER.warn(
"JMX runtime input lookup class is not available because this JRE does not support JMX. " +
"JMX lookups will not be available, continuing configuration. Ignoring " + t);
break;
case LOOKUP_KEY_WEB:
LOGGER.info("Log4j appears to be running in a Servlet environment, but there's no log4j-web module " +
"available. If you want better web container support, please add the log4j-web JAR to your " +
"web archive or server lib directory.");
break;
case LOOKUP_KEY_DOCKER: case LOOKUP_KEY_SPRING:
break;
case LOOKUP_KEY_KUBERNETES:
if (t instanceof NoClassDefFoundError) {
LOGGER.warn("Unable to create Kubernetes lookup due to missing dependency: {}", t.getMessage());
}
break;
default:
LOGGER.error("Unable to create Lookup for {}", lookupKey, t);
}
}
/**
* Resolves the specified variable. This implementation will try to extract
* a variable prefix from the given variable name (the first colon (':') is
* used as prefix separator). It then passes the name of the variable with
* the prefix stripped to the lookup object registered for this prefix. If
* no prefix can be found or if the associated lookup object cannot resolve
* this variable, the default lookup object will be used.
*
* @param event The current LogEvent or null.
* @param var the name of the variable whose value is to be looked up
* @return the value of this variable or <b>null</b> if it cannot be
* resolved
*/
@Override
public String lookup(final LogEvent event, String var) {
if (var == null) {
return null;
}
final int prefixPos = var.indexOf(PREFIX_SEPARATOR);
if (prefixPos >= 0) {
final String prefix = var.substring(0, prefixPos).toLowerCase(Locale.US);
final String name = var.substring(prefixPos + 1);
final StrLookup lookup = strLookupMap.get(prefix);
if (lookup instanceof ConfigurationAware) {
((ConfigurationAware) lookup).setConfiguration(configuration);
}
String value = null;
if (lookup != null) {
value = event == null ? lookup.lookup(name) : lookup.lookup(event, name);
}
if (value != null) {
return value;
}
var = var.substring(prefixPos + 1);
}
if (defaultLookup != null) {
return event == null ? defaultLookup.lookup(var) : defaultLookup.lookup(event, var);
}
return null;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder();
for (final String name : strLookupMap.keySet()) {
if (sb.length() == 0) {
sb.append('{');
} else {
sb.append(", ");
}
sb.append(name);
}
if (sb.length() > 0) {
sb.append('}');
}
return sb.toString();
}
}