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.Message;
29 import org.apache.logging.log4j.message.StructuredDataMessage;
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 = "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(@PluginElement("pairs") final KeyValuePair[] pairs,
105 @PluginAttr("operator") final String oper,
106 @PluginAttr("onmatch") final String match,
107 @PluginAttr("onmismatch") final String mismatch) {
108 if (pairs == null || pairs.length == 0) {
109 LOGGER.error("keys and values must be specified for the StructuredDataFilter");
110 return null;
111 }
112 final Map<String, List<String>> map = new HashMap<String, List<String>>();
113 for (final KeyValuePair pair : pairs) {
114 final String key = pair.getKey();
115 if (key == null) {
116 LOGGER.error("A null key is not valid in MapFilter");
117 continue;
118 }
119 final String value = pair.getValue();
120 if (value == null) {
121 LOGGER.error("A null value for key " + key + " is not allowed in MapFilter");
122 continue;
123 }
124 List<String> list = map.get(pair.getKey());
125 if (list != null) {
126 list.add(value);
127 } else {
128 list = new ArrayList<String>();
129 list.add(value);
130 map.put(pair.getKey(), list);
131 }
132 }
133 if (map.size() == 0) {
134 LOGGER.error("StructuredDataFilter is not configured with any valid key value pairs");
135 return null;
136 }
137 final boolean isAnd = oper == null || !oper.equalsIgnoreCase("or");
138 final Result onMatch = Result.toResult(match);
139 final Result onMismatch = Result.toResult(mismatch);
140 return new StructuredDataFilter(map, isAnd, onMatch, onMismatch);
141 }
142 }