1616 */
1717package org .apache .logging .log4j .core .filter ;
1818
19- import java .lang .reflect .Field ;
20- import java .util .Arrays ;
21- import java .util .Comparator ;
2219import java .util .Objects ;
2320import java .util .regex .Pattern ;
2421import org .apache .logging .log4j .Level ;
3128import org .apache .logging .log4j .core .config .plugins .PluginAttribute ;
3229import org .apache .logging .log4j .core .config .plugins .PluginBuilderAttribute ;
3330import org .apache .logging .log4j .core .config .plugins .PluginBuilderFactory ;
34- import org .apache .logging .log4j .core .config .plugins .PluginElement ;
3531import org .apache .logging .log4j .core .config .plugins .validation .constraints .Required ;
3632import org .apache .logging .log4j .core .util .Assert ;
3733import org .apache .logging .log4j .message .Message ;
@@ -67,21 +63,22 @@ private RegexFilter(final Builder builder) {
6763 super (builder );
6864
6965 if (Strings .isBlank (builder .regex )) {
70- throw new IllegalArgumentException ("The ' regex' attribute must not be null or empty." );
66+ throw new IllegalArgumentException ("The ` regex` attribute must not be null or empty." );
7167 }
7268
7369 this .useRawMessage = Boolean .TRUE .equals (builder .useRawMsg );
7470
7571 try {
7672 this .pattern = Pattern .compile (builder .regex );
7773 } catch (final Exception ex ) {
78- throw new IllegalArgumentException ("Unable to compile regular expression: ' " + builder .regex + "' ." , ex );
74+ throw new IllegalArgumentException ("Unable to compile regular expression: ` " + builder .regex + "` ." , ex );
7975 }
8076 }
8177
8278 /**
8379 * Returns the compiled regular-expression pattern.
8480 * @return the pattern (will never be {@code null}
81+ * @since 2.27.0
8582 */
8683 public Pattern getPattern () {
8784 return this .pattern ;
@@ -90,6 +87,7 @@ public Pattern getPattern() {
9087 /**
9188 * Returns the regular-expression.
9289 * @return the regular-expression (it may be an empty string but never {@code null})
90+ * @since 2.27.0
9391 */
9492 public String getRegex () {
9593 return this .pattern .pattern ();
@@ -98,6 +96,7 @@ public String getRegex() {
9896 /**
9997 * Returns whether the raw-message should be used.
10098 * @return {@code true} if the raw message should be used; otherwise, {@code false}
99+ * @since 2.27.0
101100 */
102101 public boolean isUseRawMessage () {
103102 return this .useRawMessage ;
@@ -189,7 +188,7 @@ public Result filter(
189188 */
190189 @ Override
191190 public Result filter (final LogEvent event ) {
192- Objects .requireNonNull (event , "The ' event' argument must not be null. " );
191+ Objects .requireNonNull (event , "event" );
193192 return filter (getMessageTextByType (event .getMessage ()));
194193 }
195194
@@ -202,7 +201,7 @@ public Result filter(final LogEvent event) {
202201 * @param msg the message
203202 * @return the {@code onMatch} result if the pattern matches; otherwise, the {@code onMismatch} result
204203 */
205- public Result filter (final @ Nullable String msg ) {
204+ Result filter (final @ Nullable String msg ) {
206205 return (msg != null && pattern .matcher (msg ).matches ()) ? onMatch : onMismatch ;
207206 }
208207
@@ -245,12 +244,13 @@ private String getMessageTextByType(final Message message) {
245244 /** {@inheritDoc} */
246245 @ Override
247246 public String toString () {
248- return "useRawMessage=" + useRawMessage + ", pattern=" + pattern . toString () ;
247+ return "useRawMessage=" + useRawMessage + ", pattern=" + pattern ;
249248 }
250249
251250 /**
252251 * Creates a new builder instance.
253252 * @return the new builder instance
253+ * @since 2.27.0
254254 */
255255 @ PluginBuilderFactory
256256 public static Builder newBuilder () {
@@ -259,17 +259,16 @@ public static Builder newBuilder() {
259259
260260 /**
261261 * A {@link RegexFilter} builder instance.
262+ * @since 2.27.0
262263 */
263264 public static final class Builder extends AbstractFilterBuilder <RegexFilter .Builder >
264265 implements org .apache .logging .log4j .core .util .Builder <RegexFilter > {
265266
266- /* NOTE: LOG4J-3086 - No patternFlags in builder - this functionality has been deprecated/removed. */
267-
268267 /**
269268 * The regular expression to match.
270269 */
271270 @ PluginBuilderAttribute
272- @ Required (message = "No ' regex' provided for RegexFilter" )
271+ @ Required (message = "No ` regex` provided for ` RegexFilter` " )
273272 private @ Nullable String regex ;
274273
275274 /**
@@ -292,7 +291,7 @@ private Builder() {
292291 * @return this builder
293292 */
294293 public Builder setRegex (final String regex ) {
295- this .regex = Assert .requireNonEmpty (regex , "The ' regex' attribute must not be null or empty." );
294+ this .regex = Assert .requireNonEmpty (regex , "The ` regex` attribute must not be null or empty." );
296295 return this ;
297296 }
298297
@@ -311,7 +310,7 @@ public Builder setUseRawMsg(final boolean useRawMsg) {
311310 /** {@inheritDoc} */
312311 @ Override
313312 public boolean isValid () {
314- return ( Strings .isNotEmpty (this .regex ) );
313+ return Strings .isNotEmpty (this .regex );
315314 }
316315
317316 /**
@@ -320,83 +319,47 @@ public boolean isValid() {
320319 * @return the created {@link RegexFilter} or {@code null} if the builder is misconfigured
321320 */
322321 @ Override
323- public @ Nullable RegexFilter build () {
322+ public RegexFilter build () {
324323
325324 // validate the "regex" attribute
326325 if (Strings .isEmpty (this .regex )) {
327- LOGGER . error ( "Unable to create RegexFilter: The 'regex' attribute be set to a non-empty String." );
328- return null ;
326+ throw new IllegalArgumentException (
327+ "Unable to create `RegexFilter`: The `regex` attribute be set to a non-empty string." ) ;
329328 }
330329
331330 // build with *safety* to not throw exceptions
332331 try {
333332 return new RegexFilter (this );
334333 } catch (final Exception ex ) {
335- LOGGER .error ("Unable to create RegexFilter. {}" , ex .getMessage (), ex );
336- return null ;
334+ throw new IllegalArgumentException ("Unable to create `RegexFilter`." , ex );
337335 }
338336 }
339337 }
340338
341- /*
342- * DEPRECATIONS:
343- * The constructor/fields/methods below have been deprecated.
344- * - the 'create***' factory methods should no longer be used - use the builder instead
345- * - pattern-flags should now be passed via the regular expression itself
346- */
347-
348- /**
349- * @deprecated pattern flags have been deprecated - they can just be included in the regex-expression.
350- */
351- @ Deprecated
352- private static final int DEFAULT_PATTERN_FLAGS = 0 ;
353-
354- /**
355- * @deprecated - pattern flags no longer supported.
356- */
357- @ Deprecated
358- private String [] patternFlags = new String [0 ];
359-
360339 /**
361340 * @deprecated use {@link RegexFilter.Builder} instead
362341 */
342+ @ SuppressWarnings ("unused" )
363343 @ Deprecated
364- @ SuppressWarnings ("MagicConstant" )
365344 private RegexFilter (
366345 final String regex ,
367346 final boolean useRawMessage ,
368- final @ Nullable String @ Nullable [] patternFlags ,
347+ // `patternFlags` has never worked, and removed in `2.27.0`.
348+ // We're keeping this field for binary backward compatibility.
349+ final @ Nullable String @ Nullable [] patternFlags ,
369350 final @ Nullable Result onMatch ,
370351 final @ Nullable Result onMismatch ) {
371352 super (onMatch , onMismatch );
372- Objects .requireNonNull (regex , "The 'regex' argument must be provided for RegexFilter" );
373- this .patternFlags = patternFlags == null ? new String [0 ] : patternFlags .clone ();
374- try {
375- int flags = toPatternFlags (this .patternFlags );
376- this .pattern = Pattern .compile (regex , flags );
377- } catch (final Exception ex ) {
378- throw new IllegalArgumentException ("Unable to compile regular expression: '" + regex + "'." , ex );
379- }
353+ Objects .requireNonNull (regex , "regex" );
354+ this .pattern = Pattern .compile (regex );
380355 this .useRawMessage = useRawMessage ;
381356 }
382357
383- /**
384- * Returns the pattern-flags applied to the regular-expression when compiling the pattern.
385- *
386- * @return the pattern-flags (maybe empty but never {@code null}
387- * @deprecated pattern-flags are no longer supported
388- */
389- @ Deprecated
390- public String [] getPatternFlags () {
391- return this .patternFlags .clone ();
392- }
393-
394358 /**
395359 * Creates a Filter that matches a regular expression.
396360 *
397361 * @param regex The regular expression to match.
398- * @param patternFlags An array of Strings where each String is a {@link Pattern#compile(String, int)} compilation flag.
399- * (no longer used - pattern flags can be embedded in regex-expression.
362+ * @param patternFlags Ignored, kept for backward compatibility.
400363 * @param useRawMsg If {@code true}, for {@link ParameterizedMessage}, {@link StringFormattedMessage},
401364 * and {@link MessageFormatMessage}, the message format pattern; for {@link StructuredDataMessage},
402365 * the message field will be used as the match target.
@@ -409,43 +372,15 @@ public String[] getPatternFlags() {
409372 */
410373 @ Deprecated
411374 public static RegexFilter createFilter (
412- // @formatter:off
413375 @ PluginAttribute ("regex" ) final String regex ,
414- @ PluginElement ("PatternFlags" ) final String @ Nullable [] patternFlags ,
376+ // `patternFlags` has never worked, and removed in `2.27.0`.
377+ // We're keeping this field for binary backward compatibility.
378+ final String @ Nullable [] patternFlags ,
415379 @ PluginAttribute ("useRawMsg" ) final @ Nullable Boolean useRawMsg ,
416380 @ PluginAttribute ("onMatch" ) final @ Nullable Result match ,
417381 @ PluginAttribute ("onMismatch" ) final @ Nullable Result mismatch )
418- // @formatter:on
419382 throws IllegalArgumentException , IllegalAccessException {
420-
421- // LOG4J-3086 - pattern-flags can be embedded in RegEx expression
422- Objects .requireNonNull (regex , "The 'regex' argument must not be null." );
423-
383+ Objects .requireNonNull (regex , "regex" );
424384 return new RegexFilter (regex , Boolean .TRUE .equals (useRawMsg ), patternFlags , match , mismatch );
425385 }
426-
427- /** @deprecated pattern flags have been deprecated - they can just be included in the regex-expression. */
428- @ Deprecated
429- private static int toPatternFlags (final String @ Nullable [] patternFlags )
430- throws IllegalArgumentException , IllegalAccessException {
431- if (patternFlags == null || patternFlags .length == 0 ) {
432- return DEFAULT_PATTERN_FLAGS ;
433- }
434- final Field [] fields = Pattern .class .getDeclaredFields ();
435- final Comparator <Field > comparator = (f1 , f2 ) -> f1 .getName ().compareTo (f2 .getName ());
436- Arrays .sort (fields , comparator );
437- final String [] fieldNames = new String [fields .length ];
438- for (int i = 0 ; i < fields .length ; i ++) {
439- fieldNames [i ] = fields [i ].getName ();
440- }
441- int flags = DEFAULT_PATTERN_FLAGS ;
442- for (final String test : patternFlags ) {
443- final int index = Arrays .binarySearch (fieldNames , test );
444- if (index >= 0 ) {
445- final Field field = fields [index ];
446- flags |= field .getInt (Pattern .class );
447- }
448- }
449- return flags ;
450- }
451386}
0 commit comments