16
16
*/
17
17
package org .apache .logging .log4j .core .filter ;
18
18
19
- import java .lang .reflect .Field ;
20
- import java .util .Arrays ;
21
- import java .util .Comparator ;
22
19
import java .util .regex .Matcher ;
23
20
import java .util .regex .Pattern ;
24
21
import org .apache .logging .log4j .Level ;
25
22
import org .apache .logging .log4j .Marker ;
26
23
import org .apache .logging .log4j .core .Filter ;
27
24
import org .apache .logging .log4j .core .LogEvent ;
28
25
import org .apache .logging .log4j .core .Logger ;
26
+ import org .apache .logging .log4j .core .config .Node ;
27
+ import org .apache .logging .log4j .core .config .plugins .Plugin ;
28
+ import org .apache .logging .log4j .core .config .plugins .PluginAttribute ;
29
+ import org .apache .logging .log4j .core .config .plugins .PluginFactory ;
29
30
import org .apache .logging .log4j .message .Message ;
30
31
import org .apache .logging .log4j .message .ParameterizedMessage ;
31
32
import org .apache .logging .log4j .plugins .Configurable ;
45
46
@ Plugin
46
47
public final class RegexFilter extends AbstractFilter {
47
48
48
- private static final int DEFAULT_PATTERN_FLAGS = 0 ;
49
49
private final Pattern pattern ;
50
50
private final boolean useRawMessage ;
51
51
@@ -101,10 +101,7 @@ private Result filter(final String msg) {
101
101
102
102
@ Override
103
103
public String toString () {
104
- final StringBuilder sb = new StringBuilder ();
105
- sb .append ("useRaw=" ).append (useRawMessage );
106
- sb .append (", pattern=" ).append (pattern .toString ());
107
- return sb .toString ();
104
+ return "useRaw=" + useRawMessage + ", pattern=" + pattern .toString ();
108
105
}
109
106
110
107
/**
@@ -114,54 +111,72 @@ public String toString() {
114
111
* The regular expression to match.
115
112
* @param patternFlags
116
113
* An array of Strings where each String is a {@link Pattern#compile(String, int)} compilation flag.
114
+ * (no longer used - pattern flags can be embedded in regex-expression.
117
115
* @param useRawMsg
118
116
* If true, the raw message will be used, otherwise the formatted message will be used.
119
117
* @param onMatch
120
118
* The action to perform when a match occurs.
121
119
* @param onMismatch
122
120
* The action to perform when a mismatch occurs.
123
121
* @return The RegexFilter.
124
- * @throws IllegalAccessException
125
- * @throws IllegalArgumentException
122
+ * @throws IllegalAccessException When there is no access to the definition of the specified member.
123
+ * @throws IllegalArgumentException When passed an illegal or inappropriate argument.
124
+ * @deprecated use {@link #createFilter(String, Boolean, Result, Result)}
125
+ */
126
+ @ Deprecated
127
+ // TODO Consider refactoring to use AbstractFilter.AbstractFilterBuilder
128
+ public static RegexFilter createFilter (
129
+ // @formatter:off
130
+ @ PluginAttribute ("regex" ) final String regex ,
131
+ final String [] patternFlags ,
132
+ @ PluginAttribute ("useRawMsg" ) final Boolean useRawMsg ,
133
+ @ PluginAttribute ("onMatch" ) final Result match ,
134
+ @ PluginAttribute ("onMismatch" ) final Result mismatch )
135
+ // @formatter:on
136
+ throws IllegalArgumentException , IllegalAccessException {
137
+
138
+ // LOG4J-3086 - pattern-flags can be embedded in RegEx expression
139
+
140
+ return createFilter (regex , useRawMsg , match , mismatch );
141
+ }
142
+
143
+ /**
144
+ * Creates a Filter that matches a regular expression.
145
+ *
146
+ * @param regex
147
+ * The regular expression to match.
148
+ * @param useRawMsg
149
+ * If {@code true}, for {@link ParameterizedMessage}, {@link StringFormattedMessage}, and {@link MessageFormatMessage}, the message format pattern; for {@link StructuredDataMessage}, the message field will be used as the match target.
150
+ * @param match
151
+ * The action to perform when a match occurs.
152
+ * @param mismatch
153
+ * The action to perform when a mismatch occurs.
154
+ * @return The RegexFilter.
155
+ * @throws IllegalAccessException When there is no access to the definition of the specified member.
156
+ * @throws IllegalArgumentException When passed an illegal or inappropriate argument.
126
157
*/
127
158
// TODO Consider refactoring to use AbstractFilter.AbstractFilterBuilder
128
159
@ PluginFactory
129
160
public static RegexFilter createFilter (
130
161
// @formatter:off
131
- @ PluginAttribute final String regex ,
132
- @ PluginElement final String [] patternFlags ,
133
- @ PluginAttribute final Boolean useRawMsg ,
134
- @ PluginAttribute final Result onMatch ,
135
- @ PluginAttribute final Result onMismatch )
162
+ @ PluginAttribute ("regex" ) final String regex ,
163
+ @ PluginAttribute ("useRawMsg" ) final Boolean useRawMsg ,
164
+ @ PluginAttribute ("onMatch" ) final Result match ,
165
+ @ PluginAttribute ("onMismatch" ) final Result mismatch )
136
166
// @formatter:on
137
167
throws IllegalArgumentException , IllegalAccessException {
168
+ boolean raw = Boolean .TRUE .equals (useRawMsg );
138
169
if (regex == null ) {
139
170
LOGGER .error ("A regular expression must be provided for RegexFilter" );
140
171
return null ;
141
172
}
142
- return new RegexFilter (useRawMsg , Pattern .compile (regex , toPatternFlags (patternFlags )), onMatch , onMismatch );
143
- }
144
-
145
- private static int toPatternFlags (final String [] patternFlags )
146
- throws IllegalArgumentException , IllegalAccessException {
147
- if (patternFlags == null || patternFlags .length == 0 ) {
148
- return DEFAULT_PATTERN_FLAGS ;
149
- }
150
- final Field [] fields = Pattern .class .getDeclaredFields ();
151
- final Comparator <Field > comparator = (f1 , f2 ) -> f1 .getName ().compareTo (f2 .getName ());
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
- }
173
+ final Pattern pattern ;
174
+ try {
175
+ pattern = Pattern .compile (regex );
176
+ } catch (final Exception ex ) {
177
+ LOGGER .error ("Unable to compile regular expression: {}" , regex , ex );
178
+ return null ;
164
179
}
165
- return flags ;
180
+ return new RegexFilter ( raw , pattern , match , mismatch ) ;
166
181
}
167
182
}
0 commit comments