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.lang.reflect.Field;
20 import java.util.Arrays;
21 import java.util.Comparator;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24
25 import org.apache.logging.log4j.Level;
26 import org.apache.logging.log4j.Marker;
27 import org.apache.logging.log4j.core.Filter;
28 import org.apache.logging.log4j.core.LogEvent;
29 import org.apache.logging.log4j.core.Logger;
30 import org.apache.logging.log4j.core.config.Node;
31 import org.apache.logging.log4j.core.config.plugins.Plugin;
32 import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
33 import org.apache.logging.log4j.core.config.plugins.PluginElement;
34 import org.apache.logging.log4j.core.config.plugins.PluginFactory;
35 import org.apache.logging.log4j.message.Message;
36
37
38
39
40
41
42
43
44 @Plugin(name = "RegexFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
45 public final class RegexFilter extends AbstractFilter {
46
47 private static final int DEFAULT_PATTERN_FLAGS = 0;
48 private final Pattern pattern;
49 private final boolean useRawMessage;
50
51 private RegexFilter(final boolean raw, final Pattern pattern, final Result onMatch, final Result onMismatch) {
52 super(onMatch, onMismatch);
53 this.pattern = pattern;
54 this.useRawMessage = raw;
55 }
56
57 @Override
58 public Result filter(final Logger logger, final Level level, final Marker marker, final String msg,
59 final Object... params) {
60 return filter(msg);
61 }
62
63 @Override
64 public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg,
65 final Throwable t) {
66 if (msg == null) {
67 return onMismatch;
68 }
69 return filter(msg.toString());
70 }
71
72 @Override
73 public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
74 final Throwable t) {
75 if (msg == null) {
76 return onMismatch;
77 }
78 final String text = useRawMessage ? msg.getFormat() : msg.getFormattedMessage();
79 return filter(text);
80 }
81
82 @Override
83 public Result filter(final LogEvent event) {
84 final String text = useRawMessage ? event.getMessage().getFormat() : event.getMessage().getFormattedMessage();
85 return filter(text);
86 }
87
88 private Result filter(final String msg) {
89 if (msg == null) {
90 return onMismatch;
91 }
92 final Matcher m = pattern.matcher(msg);
93 return m.matches() ? onMatch : onMismatch;
94 }
95
96 @Override
97 public String toString() {
98 final StringBuilder sb = new StringBuilder();
99 sb.append("useRaw=").append(useRawMessage);
100 sb.append(", pattern=").append(pattern.toString());
101 return sb.toString();
102 }
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122 @PluginFactory
123 public static RegexFilter createFilter(
124
125 @PluginAttribute("regex") final String regex,
126 @PluginElement("PatternFlags") final String[] patternFlags,
127 @PluginAttribute("useRawMsg") final Boolean useRawMsg,
128 @PluginAttribute("onMatch") final Result match,
129 @PluginAttribute("onMismatch") final Result mismatch)
130
131 throws IllegalArgumentException, IllegalAccessException {
132 if (regex == null) {
133 LOGGER.error("A regular expression must be provided for RegexFilter");
134 return null;
135 }
136 return new RegexFilter(useRawMsg, Pattern.compile(regex, toPatternFlags(patternFlags)), match, mismatch);
137 }
138
139 private static int toPatternFlags(final String[] patternFlags) throws IllegalArgumentException,
140 IllegalAccessException {
141 if (patternFlags == null || patternFlags.length == 0) {
142 return DEFAULT_PATTERN_FLAGS;
143 }
144 final Field[] fields = Pattern.class.getDeclaredFields();
145 final Comparator<Field> comparator = new Comparator<Field>() {
146
147 @Override
148 public int compare(final Field f1, final Field f2) {
149 return f1.getName().compareTo(f2.getName());
150 }
151 };
152 Arrays.sort(fields, comparator);
153 final String[] fieldNames = new String[fields.length];
154 for (int i = 0; i < fields.length; i++) {
155 fieldNames[i] = fields[i].getName();
156 }
157 int flags = DEFAULT_PATTERN_FLAGS;
158 for (final String test : patternFlags) {
159 final int index = Arrays.binarySearch(fieldNames, test);
160 if (index >= 0) {
161 final Field field = fields[index];
162 flags |= field.getInt(Pattern.class);
163 }
164 }
165 return flags;
166 }
167 }