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.Map;
20  
21  import org.apache.logging.log4j.core.LogEvent;
22  import org.apache.logging.log4j.core.config.plugins.Plugin;
23  import org.apache.logging.log4j.message.MapMessage;
24  
25  /**
26   * The basis for a lookup based on a Map.
27   */
28  @Plugin(name = "map", category = "Lookup")
29  public class MapLookup implements StrLookup {
30      
31      /**
32       * Map keys are variable names and value.
33       */
34      private final Map<String, String> map;
35  
36      /**
37       * Creates a new instance backed by a Map. Used by the default lookup.
38       *
39       * @param map the map of keys to values, may be null
40       */
41      public MapLookup(final Map<String, String> map) {
42          this.map = map;
43      }
44  
45      /**
46       * Constructor when used directly as a plugin.
47       */
48      public MapLookup() {
49          this.map = null;
50      }
51  
52      /**
53       * Looks up a String key to a String value using the map.
54       * <p>
55       * If the map is null, then null is returned.
56       * The map result object is converted to a string using toString().
57       * </p>
58       *
59       * @param key the key to be looked up, may be null
60       * @return the matching value, null if no match
61       */
62      @Override
63      public String lookup(final String key) {
64          if (map == null) {
65              return null;
66          }
67          return map.get(key);
68      }
69  
70      @Override
71      public String lookup(final LogEvent event, final String key) {
72          if (map == null && !(event.getMessage() instanceof MapMessage)) {
73              return null;
74          }
75          if (map != null && map.containsKey(key)) {
76              final String obj = map.get(key);
77              if (obj != null) {
78                  return obj;
79              }
80          }
81          if (event.getMessage() instanceof MapMessage) {
82              return ((MapMessage) event.getMessage()).get(key);
83          }
84          return null;
85      }
86  }