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.filter;
18  
19  import org.apache.logging.log4j.Level;
20  import org.apache.logging.log4j.Marker;
21  import org.apache.logging.log4j.core.LogEvent;
22  import org.apache.logging.log4j.core.Logger;
23  import org.apache.logging.log4j.core.config.plugins.Plugin;
24  import org.apache.logging.log4j.core.config.plugins.PluginAttr;
25  import org.apache.logging.log4j.core.config.plugins.PluginElement;
26  import org.apache.logging.log4j.core.config.plugins.PluginFactory;
27  import org.apache.logging.log4j.core.helpers.KeyValuePair;
28  import org.apache.logging.log4j.message.MapMessage;
29  import org.apache.logging.log4j.message.Message;
30  
31  import java.util.ArrayList;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Locale;
35  import java.util.Map;
36  
37  /**
38   * A Filter that operates on a Map.
39   */
40  @Plugin(name = "MapFilter", type = "Core", elementType = "filter", printObject = true)
41  public class MapFilter extends AbstractFilter {
42      private final Map<String, List<String>> map;
43  
44      private final boolean isAnd;
45  
46      protected MapFilter(Map<String, List<String>> map, boolean oper, Result onMatch, Result onMismatch) {
47          super(onMatch, onMismatch);
48          if (map == null) {
49              throw new NullPointerException("key cannot be null");
50          }
51          this.isAnd = oper;
52          this.map = map;
53      }
54  
55      @Override
56      public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) {
57          if (msg instanceof MapMessage) {
58              return filter(((MapMessage) msg).getData()) ? onMatch : onMismatch;
59          }
60          return Result.NEUTRAL;
61      }
62  
63      @Override
64      public Result filter(LogEvent event) {
65          Message msg = event.getMessage();
66          if (msg instanceof MapMessage) {
67              return filter(((MapMessage) msg).getData()) ? onMatch : onMismatch;
68          }
69          return Result.NEUTRAL;
70      }
71  
72      protected boolean filter(Map<String, String> data) {
73          boolean match = false;
74          for (Map.Entry<String, List<String>> entry : map.entrySet()) {
75              String toMatch = data.get(entry.getKey());
76              if (toMatch != null) {
77                  match = entry.getValue().contains(toMatch);
78              } else {
79                  match = false;
80              }
81              if ((!isAnd && match) || (isAnd && !match)) {
82                  break;
83              }
84          }
85          return match;
86      }
87  
88      @Override
89      public String toString() {
90          StringBuilder sb = new StringBuilder();
91          sb.append("isAnd=").append(isAnd);
92          if (map.size() > 0) {
93              sb.append(", {");
94              boolean first = true;
95              for (Map.Entry<String, List<String>> entry : map.entrySet()) {
96                  if (!first) {
97                      sb.append(", ");
98                  }
99                  first = false;
100                 List<String> list = entry.getValue();
101                 String value = list.size() > 1 ? list.get(0) : list.toString();
102                 sb.append(entry.getKey()).append("=").append(value);
103             }
104             sb.append("}");
105         }
106         return sb.toString();
107     }
108 
109     protected boolean isAnd() {
110         return isAnd;
111     }
112 
113     protected Map<String, List<String>> getMap() {
114         return map;
115     }
116 
117     @PluginFactory
118     public static MapFilter createFilter(@PluginElement("pairs") KeyValuePair[] pairs,
119                                          @PluginAttr("operator") String oper,
120                                          @PluginAttr("onmatch") String match,
121                                          @PluginAttr("onmismatch") String mismatch) {
122         if (pairs == null || pairs.length == 0) {
123             LOGGER.error("keys and values must be specified for the MapFilter");
124             return null;
125         }
126         Map<String, List<String>> map = new HashMap<String, List<String>>();
127         for (KeyValuePair pair : pairs) {
128             String key = pair.getKey();
129             if (key == null) {
130                 LOGGER.error("A null key is not valid in MapFilter");
131                 continue;
132             }
133             String value = pair.getValue();
134             if (value == null) {
135                 LOGGER.error("A null value for key " + key + " is not allowed in MapFilter");
136                 continue;
137             }
138             List<String> list = map.get(pair.getKey());
139             if (list != null) {
140                 list.add(value);
141             } else {
142                 list = new ArrayList<String>();
143                 list.add(value);
144                 map.put(pair.getKey(), list);
145             }
146         }
147         if (map.size() == 0) {
148             LOGGER.error("MapFilter is not configured with any valid key value pairs");
149             return null;
150         }
151         boolean isAnd = oper == null || !oper.equalsIgnoreCase("or");
152         Result onMatch = Result.toResult(match);
153         Result onMismatch = Result.toResult(mismatch);
154         return new MapFilter(map, isAnd, onMatch, onMismatch);
155     }
156 }