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