1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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.Map;
35
36
37
38
39 @Plugin(name = "MapFilter", category = "Core", elementType = "filter", printObject = true)
40 public class MapFilter extends AbstractFilter {
41 private final Map<String, List<String>> map;
42
43 private final boolean isAnd;
44
45 protected MapFilter(final Map<String, List<String>> map, final boolean oper, final Result onMatch,
46 final 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(final Logger logger, final Level level, final Marker marker, final Message msg,
57 final Throwable t) {
58 if (msg instanceof MapMessage) {
59 return filter(((MapMessage) msg).getData()) ? onMatch : onMismatch;
60 }
61 return Result.NEUTRAL;
62 }
63
64 @Override
65 public Result filter(final LogEvent event) {
66 final Message msg = event.getMessage();
67 if (msg instanceof MapMessage) {
68 return filter(((MapMessage) msg).getData()) ? onMatch : onMismatch;
69 }
70 return Result.NEUTRAL;
71 }
72
73 protected boolean filter(final Map<String, String> data) {
74 boolean match = false;
75 for (final Map.Entry<String, List<String>> entry : map.entrySet()) {
76 final String toMatch = data.get(entry.getKey());
77 if (toMatch != null) {
78 match = entry.getValue().contains(toMatch);
79 } else {
80 match = false;
81 }
82 if ((!isAnd && match) || (isAnd && !match)) {
83 break;
84 }
85 }
86 return match;
87 }
88
89 @Override
90 public String toString() {
91 final StringBuilder sb = new StringBuilder();
92 sb.append("isAnd=").append(isAnd);
93 if (map.size() > 0) {
94 sb.append(", {");
95 boolean first = true;
96 for (final Map.Entry<String, List<String>> entry : map.entrySet()) {
97 if (!first) {
98 sb.append(", ");
99 }
100 first = false;
101 final List<String> list = entry.getValue();
102 final String value = list.size() > 1 ? list.get(0) : list.toString();
103 sb.append(entry.getKey()).append("=").append(value);
104 }
105 sb.append("}");
106 }
107 return sb.toString();
108 }
109
110 protected boolean isAnd() {
111 return isAnd;
112 }
113
114 protected Map<String, List<String>> getMap() {
115 return map;
116 }
117
118 @PluginFactory
119 public static MapFilter createFilter(@PluginElement("pairs") final KeyValuePair[] pairs,
120 @PluginAttr("operator") final String oper,
121 @PluginAttr("onmatch") final String match,
122 @PluginAttr("onmismatch") final String mismatch) {
123 if (pairs == null || pairs.length == 0) {
124 LOGGER.error("keys and values must be specified for the MapFilter");
125 return null;
126 }
127 final Map<String, List<String>> map = new HashMap<String, List<String>>();
128 for (final KeyValuePair pair : pairs) {
129 final String key = pair.getKey();
130 if (key == null) {
131 LOGGER.error("A null key is not valid in MapFilter");
132 continue;
133 }
134 final String value = pair.getValue();
135 if (value == null) {
136 LOGGER.error("A null value for key " + key + " is not allowed in MapFilter");
137 continue;
138 }
139 List<String> list = map.get(pair.getKey());
140 if (list != null) {
141 list.add(value);
142 } else {
143 list = new ArrayList<String>();
144 list.add(value);
145 map.put(pair.getKey(), list);
146 }
147 }
148 if (map.size() == 0) {
149 LOGGER.error("MapFilter is not configured with any valid key value pairs");
150 return null;
151 }
152 final boolean isAnd = oper == null || !oper.equalsIgnoreCase("or");
153 final Result onMatch = Result.toResult(match);
154 final Result onMismatch = Result.toResult(mismatch);
155 return new MapFilter(map, isAnd, onMatch, onMismatch);
156 }
157 }