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 .Objects ;
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 ;
29
- import org .apache .logging .log4j .core .config .Node ;
30
- import org .apache .logging .log4j .core .config .plugins .Plugin ;
31
- import org .apache .logging .log4j .core .config .plugins .PluginAttribute ;
32
- import org .apache .logging .log4j .core .config .plugins .PluginBuilderAttribute ;
33
- import org .apache .logging .log4j .core .config .plugins .PluginBuilderFactory ;
34
- import org .apache .logging .log4j .core .config .plugins .PluginElement ;
35
- import org .apache .logging .log4j .core .config .plugins .validation .constraints .Required ;
36
- import org .apache .logging .log4j .core .util .Assert ;
37
26
import org .apache .logging .log4j .message .Message ;
38
27
import org .apache .logging .log4j .message .ParameterizedMessage ;
39
28
import org .apache .logging .log4j .message .StringFormattedMessage ;
40
29
import org .apache .logging .log4j .message .StructuredDataMessage ;
30
+ import org .apache .logging .log4j .plugins .Configurable ;
31
+ import org .apache .logging .log4j .plugins .Plugin ;
32
+ import org .apache .logging .log4j .plugins .PluginBuilderAttribute ;
33
+ import org .apache .logging .log4j .plugins .PluginFactory ;
34
+ import org .apache .logging .log4j .plugins .util .Assert ;
35
+ import org .apache .logging .log4j .plugins .validation .constraints .Required ;
41
36
import org .apache .logging .log4j .util .Strings ;
42
37
import org .jspecify .annotations .NullMarked ;
43
38
import org .jspecify .annotations .Nullable ;
44
39
45
40
/**
46
41
* This filter returns the {@code onMatch} result if the message exactly matches the configured
47
42
* "{@code regex}" regular-expression pattern; otherwise, it returns the {@code onMismatch} result.
43
+ * <p>
44
+ * The "useRawMsg" attribute can be used to indicate whether the regular expression should be applied to
45
+ * the result of calling Message.getMessageFormat (true) or Message.getFormattedMessage() (false).
46
+ * The default is {@code false}.
47
+ * </p>
48
48
*/
49
- @ Plugin ( name = "RegexFilter" , category = Node . CATEGORY , elementType = Filter .ELEMENT_TYPE , printObject = true )
49
+ @ Configurable ( elementType = Filter .ELEMENT_TYPE , printObject = true )
50
50
@ NullMarked
51
+ @ Plugin
51
52
public final class RegexFilter extends AbstractFilter {
52
53
53
54
/** The pattern compiled from the regular-expression. */
@@ -65,6 +66,10 @@ private RegexFilter(final Builder builder) {
65
66
66
67
super (builder );
67
68
69
+ // NOTE: the constructor throws exceptions but is only called from Builder#build() where *null*
70
+ // should be returned for a misconfigured builder. *If* an exception is thrown here
71
+ // it will be caught and logged in the builder and not propagated by returning *null*.
72
+
68
73
if (Strings .isBlank (builder .regex )) {
69
74
throw new IllegalArgumentException ("The 'regex' attribute must not be null or empty." );
70
75
}
@@ -128,6 +133,7 @@ public Result filter(
128
133
return (useRawMessage || params == null || params .length == 0 )
129
134
? filter (msg )
130
135
: filter (ParameterizedMessage .format (msg , params ));
136
+
131
137
}
132
138
133
139
/**
@@ -212,7 +218,6 @@ public Result filter(final @Nullable String msg) {
212
218
* will be returned.
213
219
* </p>
214
220
* <ul>
215
- * <li>{@link MessageFormatMessage}</li>
216
221
* <li>{@link ParameterizedMessage}</li>
217
222
* <li>{@link StringFormattedMessage}</li>
218
223
* <li>{@link StructuredDataMessage}</li>
@@ -235,7 +240,6 @@ private String getMessageTextByType(final Message message) {
235
240
return useRawMessage
236
241
&& (message instanceof ParameterizedMessage
237
242
|| message instanceof StringFormattedMessage
238
- || message instanceof MessageFormatMessage
239
243
|| message instanceof StructuredDataMessage )
240
244
? message .getFormat ()
241
245
: message .getFormattedMessage ();
@@ -244,14 +248,14 @@ private String getMessageTextByType(final Message message) {
244
248
/** {@inheritDoc} */
245
249
@ Override
246
250
public String toString () {
247
- return "useRawMessage=" + useRawMessage + ", pattern=" + pattern . toString () ;
251
+ return "useRawMessage=" + useRawMessage + ", pattern=" + pattern ;
248
252
}
249
253
250
254
/**
251
255
* Creates a new builder instance.
252
256
* @return the new builder instance
253
257
*/
254
- @ PluginBuilderFactory
258
+ @ PluginFactory
255
259
public static Builder newBuilder () {
256
260
return new Builder ();
257
261
}
@@ -260,7 +264,7 @@ public static Builder newBuilder() {
260
264
* A {@link RegexFilter} builder instance.
261
265
*/
262
266
public static final class Builder extends AbstractFilterBuilder <RegexFilter .Builder >
263
- implements org .apache .logging .log4j .core .util .Builder <RegexFilter > {
267
+ implements org .apache .logging .log4j .plugins .util .Builder <RegexFilter > {
264
268
265
269
/* NOTE: LOG4J-3086 - No patternFlags in builder - this functionality has been deprecated/removed. */
266
270
@@ -272,8 +276,8 @@ public static final class Builder extends AbstractFilterBuilder<RegexFilter.Buil
272
276
private @ Nullable String regex ;
273
277
274
278
/**
275
- * If {@code true}, for {@link ParameterizedMessage}, {@link StringFormattedMessage},
276
- * and {@link MessageFormatMessage}, the message format pattern; for {@link StructuredDataMessage},
279
+ * If {@code true}, for {@link ParameterizedMessage} / {@link StringFormattedMessage},
280
+ * the message format pattern will be used as the match target, and for {@link StructuredDataMessage}
277
281
* the message field will be used as the match target.
278
282
*/
279
283
@ PluginBuilderAttribute
@@ -308,7 +312,6 @@ public Builder setUseRawMsg(final boolean useRawMsg) {
308
312
}
309
313
310
314
/** {@inheritDoc} */
311
- @ Override
312
315
public boolean isValid () {
313
316
return (Strings .isNotEmpty (this .regex ));
314
317
}
@@ -337,114 +340,4 @@ public boolean isValid() {
337
340
}
338
341
}
339
342
340
- /*
341
- * DEPRECATIONS:
342
- * The constructor/fields/methods below have been deprecated.
343
- * - the 'create***' factory methods should no longer be used - use the builder instead
344
- * - pattern-flags should now be passed via the regular expression itself
345
- */
346
-
347
- /**
348
- * @deprecated pattern flags have been deprecated - they can just be included in the regex-expression.
349
- */
350
- @ Deprecated
351
- private static final int DEFAULT_PATTERN_FLAGS = 0 ;
352
-
353
- /**
354
- * @deprecated - pattern flags no longer supported.
355
- */
356
- @ Deprecated
357
- private String [] patternFlags = new String [0 ];
358
-
359
- /**
360
- * @deprecated use {@link RegexFilter.Builder} instead
361
- */
362
- @ Deprecated
363
- @ SuppressWarnings ("MagicConstant" )
364
- private RegexFilter (
365
- final String regex ,
366
- final boolean useRawMessage ,
367
- final @ Nullable String @ Nullable [] patternFlags ,
368
- final @ Nullable Result onMatch ,
369
- final @ Nullable Result onMismatch ) {
370
- super (onMatch , onMismatch );
371
- Objects .requireNonNull (regex , "The 'regex' argument must be provided for RegexFilter" );
372
- this .patternFlags = patternFlags == null ? new String [0 ] : patternFlags .clone ();
373
- try {
374
- int flags = toPatternFlags (this .patternFlags );
375
- this .pattern = Pattern .compile (regex , flags );
376
- } catch (final Exception ex ) {
377
- throw new IllegalArgumentException ("Unable to compile regular expression: '" + regex + "'." , ex );
378
- }
379
- this .useRawMessage = useRawMessage ;
380
- }
381
-
382
- /**
383
- * Returns the pattern-flags applied to the regular-expression when compiling the pattern.
384
- *
385
- * @return the pattern-flags (maybe empty but never {@code null}
386
- * @deprecated pattern-flags are no longer supported
387
- */
388
- @ Deprecated
389
- public String [] getPatternFlags () {
390
- return this .patternFlags .clone ();
391
- }
392
-
393
- /**
394
- * Creates a Filter that matches a regular expression.
395
- *
396
- * @param regex The regular expression to match.
397
- * @param patternFlags An array of Strings where each String is a {@link Pattern#compile(String, int)} compilation flag.
398
- * (no longer used - pattern flags can be embedded in regex-expression.
399
- * @param useRawMsg If {@code true}, for {@link ParameterizedMessage}, {@link StringFormattedMessage},
400
- * and {@link MessageFormatMessage}, the message format pattern; for {@link StructuredDataMessage},
401
- * the message field will be used as the match target.
402
- * @param match The action to perform when a match occurs.
403
- * @param mismatch The action to perform when a mismatch occurs.
404
- * @return The RegexFilter.
405
- * @throws IllegalAccessException When there is no access to the definition of the specified member.
406
- * @throws IllegalArgumentException When passed an illegal or inappropriate argument.
407
- * @deprecated use {@link #newBuilder} to instantiate builder
408
- */
409
- @ Deprecated
410
- public static RegexFilter createFilter (
411
- // @formatter:off
412
- @ PluginAttribute ("regex" ) final String regex ,
413
- @ PluginElement ("PatternFlags" ) final String @ Nullable [] patternFlags ,
414
- @ PluginAttribute ("useRawMsg" ) final @ Nullable Boolean useRawMsg ,
415
- @ PluginAttribute ("onMatch" ) final @ Nullable Result match ,
416
- @ PluginAttribute ("onMismatch" ) final @ Nullable Result mismatch )
417
- // @formatter:on
418
- throws IllegalArgumentException , IllegalAccessException {
419
-
420
- // LOG4J-3086 - pattern-flags can be embedded in RegEx expression
421
- Objects .requireNonNull (regex , "The 'regex' argument must not be null." );
422
-
423
- return new RegexFilter (regex , Boolean .TRUE .equals (useRawMsg ), patternFlags , match , mismatch );
424
- }
425
-
426
- /** @deprecated pattern flags have been deprecated - they can just be included in the regex-expression. */
427
- @ Deprecated
428
- private static int toPatternFlags (final String @ Nullable [] patternFlags )
429
- throws IllegalArgumentException , IllegalAccessException {
430
- if (patternFlags == null || patternFlags .length == 0 ) {
431
- return DEFAULT_PATTERN_FLAGS ;
432
- }
433
- final Field [] fields = Pattern .class .getDeclaredFields ();
434
- final Comparator <Field > comparator = (f1 , f2 ) -> f1 .getName ().compareTo (f2 .getName ());
435
- Arrays .sort (fields , comparator );
436
- final String [] fieldNames = new String [fields .length ];
437
- for (int i = 0 ; i < fields .length ; i ++) {
438
- fieldNames [i ] = fields [i ].getName ();
439
- }
440
- int flags = DEFAULT_PATTERN_FLAGS ;
441
- for (final String test : patternFlags ) {
442
- final int index = Arrays .binarySearch (fieldNames , test );
443
- if (index >= 0 ) {
444
- final Field field = fields [index ];
445
- flags |= field .getInt (Pattern .class );
446
- }
447
- }
448
- return flags ;
449
- }
450
343
}
0 commit comments