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