1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.lookup;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.apache.logging.log4j.Logger;
24 import org.apache.logging.log4j.core.LogEvent;
25 import org.apache.logging.log4j.core.config.ConfigurationAware;
26 import org.apache.logging.log4j.core.config.plugins.util.PluginManager;
27 import org.apache.logging.log4j.core.config.plugins.util.PluginType;
28 import org.apache.logging.log4j.core.util.Loader;
29 import org.apache.logging.log4j.core.util.ReflectionUtil;
30 import org.apache.logging.log4j.status.StatusLogger;
31
32
33
34
35 public class Interpolator extends AbstractConfigurationAwareLookup {
36
37 private static final Logger LOGGER = StatusLogger.getLogger();
38
39
40 private static final char PREFIX_SEPARATOR = ':';
41
42 private final Map<String, StrLookup> lookups = new HashMap<>();
43
44 private final StrLookup defaultLookup;
45
46 public Interpolator(final StrLookup defaultLookup) {
47 this(defaultLookup, null);
48 }
49
50
51
52
53
54
55
56
57 public Interpolator(final StrLookup defaultLookup, final List<String> pluginPackages) {
58 this.defaultLookup = defaultLookup == null ? new MapLookup(new HashMap<String, String>()) : defaultLookup;
59 final PluginManager manager = new PluginManager(CATEGORY);
60 manager.collectPlugins(pluginPackages);
61 final Map<String, PluginType<?>> plugins = manager.getPlugins();
62
63 for (final Map.Entry<String, PluginType<?>> entry : plugins.entrySet()) {
64 try {
65 final Class<? extends StrLookup> clazz = entry.getValue().getPluginClass().asSubclass(StrLookup.class);
66 lookups.put(entry.getKey(), ReflectionUtil.instantiate(clazz));
67 } catch (final Exception ex) {
68 LOGGER.error("Unable to create Lookup for {}", entry.getKey(), ex);
69 }
70 }
71 }
72
73
74
75
76 public Interpolator() {
77 this((Map<String, String>) null);
78 }
79
80
81
82
83 public Interpolator(final Map<String, String> properties) {
84 this.defaultLookup = new MapLookup(properties == null ? new HashMap<String, String>() : properties);
85
86 lookups.put("log4j", new Log4jLookup());
87 lookups.put("sys", new SystemPropertiesLookup());
88 lookups.put("env", new EnvironmentLookup());
89 lookups.put("main", MainMapLookup.MAIN_SINGLETON);
90 lookups.put("marker", new MarkerLookup());
91 lookups.put("java", new JavaLookup());
92
93 try {
94
95 lookups.put("jndi",
96 Loader.newCheckedInstanceOf("org.apache.logging.log4j.core.lookup.JndiLookup", StrLookup.class));
97 } catch (final Throwable e) {
98
99 LOGGER.warn(
100 "JNDI lookup class is not available because this JRE does not support JNDI. JNDI string lookups will not be available, continuing configuration.",
101 e);
102 }
103
104 try {
105
106 lookups.put("jvmrunargs",
107 Loader.newCheckedInstanceOf("org.apache.logging.log4j.core.lookup.JmxRuntimeInputArgumentsLookup", StrLookup.class));
108 } catch (final Throwable e) {
109
110 LOGGER.warn(
111 "JMX runtime input lookup class is not available because this JRE does not support JMX. JMX lookups will not be available, continuing configuration.",
112 e);
113 }
114 lookups.put("date", new DateLookup());
115 lookups.put("ctx", new ContextMapLookup());
116 if (Loader.isClassAvailable("javax.servlet.ServletContext")) {
117 try {
118 lookups.put("web",
119 Loader.newCheckedInstanceOf("org.apache.logging.log4j.web.WebLookup", StrLookup.class));
120 } catch (final Exception ignored) {
121 LOGGER.info("Log4j appears to be running in a Servlet environment, but there's no log4j-web module " +
122 "available. If you want better web container support, please add the log4j-web JAR to your " +
123 "web archive or server lib directory.");
124 }
125 } else {
126 LOGGER.debug("Not in a ServletContext environment, thus not loading WebLookup plugin.");
127 }
128 }
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 @Override
144 public String lookup(final LogEvent event, String var) {
145 if (var == null) {
146 return null;
147 }
148
149 final int prefixPos = var.indexOf(PREFIX_SEPARATOR);
150 if (prefixPos >= 0) {
151 final String prefix = var.substring(0, prefixPos);
152 final String name = var.substring(prefixPos + 1);
153 final StrLookup lookup = lookups.get(prefix);
154 if (lookup instanceof ConfigurationAware) {
155 ((ConfigurationAware) lookup).setConfiguration(configuration);
156 }
157 String value = null;
158 if (lookup != null) {
159 value = event == null ? lookup.lookup(name) : lookup.lookup(event, name);
160 }
161
162 if (value != null) {
163 return value;
164 }
165 var = var.substring(prefixPos + 1);
166 }
167 if (defaultLookup != null) {
168 return event == null ? defaultLookup.lookup(var) : defaultLookup.lookup(event, var);
169 }
170 return null;
171 }
172
173 @Override
174 public String toString() {
175 final StringBuilder sb = new StringBuilder();
176 for (final String name : lookups.keySet()) {
177 if (sb.length() == 0) {
178 sb.append('{');
179 } else {
180 sb.append(", ");
181 }
182
183 sb.append(name);
184 }
185 if (sb.length() > 0) {
186 sb.append('}');
187 }
188 return sb.toString();
189 }
190 }