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