Skip to content

Commit 0d348a6

Browse files
committedMar 2, 2025
Cleaned up cherry-pick from 2.x - removed deprecated API (apache#3086)
1 parent d289be3 commit 0d348a6

File tree

2 files changed

+24
-143
lines changed

2 files changed

+24
-143
lines changed
 

‎log4j-core-test/src/test/java/org/apache/logging/log4j/core/filter/RegexFilterTest.java

+1-13
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import org.junit.jupiter.api.BeforeAll;
3838
import org.junit.jupiter.api.Test;
3939

40-
public class RegexFilterTest {
40+
class RegexFilterTest {
4141
@BeforeAll
4242
static void before() {
4343
StatusLogger.getLogger().getFallbackListener().setLevel(Level.OFF);
@@ -78,18 +78,6 @@ void testThresholds() throws Exception {
7878
assertNull(filter);
7979
}
8080

81-
@Test
82-
public void testDotAllPattern() throws Exception {
83-
final String singleLine = "test single line matches";
84-
final String multiLine = "test multi line matches\nsome more lines";
85-
final RegexFilter filter = RegexFilter.createFilter(
86-
".*line.*", new String[] {"DOTALL", "COMMENTS"}, false, Filter.Result.DENY, Filter.Result.ACCEPT);
87-
final Result singleLineResult = filter.filter(null, null, null, (Object) singleLine, (Throwable) null);
88-
final Result multiLineResult = filter.filter(null, null, null, (Object) multiLine, (Throwable) null);
89-
assertThat(singleLineResult, equalTo(Result.DENY));
90-
assertThat(multiLineResult, equalTo(Result.DENY));
91-
}
92-
9381
@Test
9482
void testNoMsg() {
9583

‎log4j-core/src/main/java/org/apache/logging/log4j/core/filter/RegexFilter.java

+23-130
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,39 @@
1616
*/
1717
package org.apache.logging.log4j.core.filter;
1818

19-
import java.lang.reflect.Field;
20-
import java.util.Arrays;
21-
import java.util.Comparator;
2219
import java.util.Objects;
2320
import java.util.regex.Pattern;
2421
import org.apache.logging.log4j.Level;
2522
import org.apache.logging.log4j.Marker;
2623
import org.apache.logging.log4j.core.Filter;
2724
import org.apache.logging.log4j.core.LogEvent;
2825
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;
3726
import org.apache.logging.log4j.message.Message;
3827
import org.apache.logging.log4j.message.ParameterizedMessage;
3928
import org.apache.logging.log4j.message.StringFormattedMessage;
4029
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;
4136
import org.apache.logging.log4j.util.Strings;
4237
import org.jspecify.annotations.NullMarked;
4338
import org.jspecify.annotations.Nullable;
4439

4540
/**
4641
* This filter returns the {@code onMatch} result if the message exactly matches the configured
4742
* "{@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>
4848
*/
49-
@Plugin(name = "RegexFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
49+
@Configurable(elementType = Filter.ELEMENT_TYPE, printObject = true)
5050
@NullMarked
51+
@Plugin
5152
public final class RegexFilter extends AbstractFilter {
5253

5354
/** The pattern compiled from the regular-expression. */
@@ -65,6 +66,10 @@ private RegexFilter(final Builder builder) {
6566

6667
super(builder);
6768

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+
6873
if (Strings.isBlank(builder.regex)) {
6974
throw new IllegalArgumentException("The 'regex' attribute must not be null or empty.");
7075
}
@@ -128,6 +133,7 @@ public Result filter(
128133
return (useRawMessage || params == null || params.length == 0)
129134
? filter(msg)
130135
: filter(ParameterizedMessage.format(msg, params));
136+
131137
}
132138

133139
/**
@@ -212,7 +218,6 @@ public Result filter(final @Nullable String msg) {
212218
* will be returned.
213219
* </p>
214220
* <ul>
215-
* <li>{@link MessageFormatMessage}</li>
216221
* <li>{@link ParameterizedMessage}</li>
217222
* <li>{@link StringFormattedMessage}</li>
218223
* <li>{@link StructuredDataMessage}</li>
@@ -235,7 +240,6 @@ private String getMessageTextByType(final Message message) {
235240
return useRawMessage
236241
&& (message instanceof ParameterizedMessage
237242
|| message instanceof StringFormattedMessage
238-
|| message instanceof MessageFormatMessage
239243
|| message instanceof StructuredDataMessage)
240244
? message.getFormat()
241245
: message.getFormattedMessage();
@@ -244,14 +248,14 @@ private String getMessageTextByType(final Message message) {
244248
/** {@inheritDoc} */
245249
@Override
246250
public String toString() {
247-
return "useRawMessage=" + useRawMessage + ", pattern=" + pattern.toString();
251+
return "useRawMessage=" + useRawMessage + ", pattern=" + pattern;
248252
}
249253

250254
/**
251255
* Creates a new builder instance.
252256
* @return the new builder instance
253257
*/
254-
@PluginBuilderFactory
258+
@PluginFactory
255259
public static Builder newBuilder() {
256260
return new Builder();
257261
}
@@ -260,7 +264,7 @@ public static Builder newBuilder() {
260264
* A {@link RegexFilter} builder instance.
261265
*/
262266
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> {
264268

265269
/* NOTE: LOG4J-3086 - No patternFlags in builder - this functionality has been deprecated/removed. */
266270

@@ -272,8 +276,8 @@ public static final class Builder extends AbstractFilterBuilder<RegexFilter.Buil
272276
private @Nullable String regex;
273277

274278
/**
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}
277281
* the message field will be used as the match target.
278282
*/
279283
@PluginBuilderAttribute
@@ -308,7 +312,6 @@ public Builder setUseRawMsg(final boolean useRawMsg) {
308312
}
309313

310314
/** {@inheritDoc} */
311-
@Override
312315
public boolean isValid() {
313316
return (Strings.isNotEmpty(this.regex));
314317
}
@@ -337,114 +340,4 @@ public boolean isValid() {
337340
}
338341
}
339342

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-
}
450343
}

0 commit comments

Comments
 (0)
Please sign in to comment.