Skip to content

Commit 88fdae8

Browse files
committed
Apply review feedback shared in apache#3512
1 parent 68c8c68 commit 88fdae8

4 files changed

Lines changed: 40 additions & 111 deletions

File tree

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

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
import static org.junit.jupiter.api.Assertions.assertEquals;
2323
import static org.junit.jupiter.api.Assertions.assertFalse;
2424
import static org.junit.jupiter.api.Assertions.assertNotNull;
25-
import static org.junit.jupiter.api.Assertions.assertNull;
2625
import static org.junit.jupiter.api.Assertions.assertSame;
26+
import static org.junit.jupiter.api.Assertions.assertThrows;
2727
import static org.junit.jupiter.api.Assertions.assertTrue;
2828

2929
import org.apache.logging.log4j.Level;
@@ -72,16 +72,16 @@ void testThresholds() throws Exception {
7272
.setMessage(new SimpleMessage("test")) //
7373
.build();
7474
assertSame(Filter.Result.DENY, filter.filter(event));
75-
filter = RegexFilter.newBuilder().build();
76-
assertNull(filter);
75+
final RegexFilter.Builder filterBuilder = RegexFilter.newBuilder();
76+
assertThrows(IllegalArgumentException.class, filterBuilder::build);
7777
}
7878

7979
@Test
8080
void testDotAllPattern() throws Exception {
8181
final String singleLine = "test single line matches";
8282
final String multiLine = "test multi line matches\nsome more lines";
83-
final RegexFilter filter = RegexFilter.createFilter(
84-
".*line.*", new String[] {"DOTALL", "COMMENTS"}, false, Filter.Result.DENY, Filter.Result.ACCEPT);
83+
final RegexFilter filter =
84+
RegexFilter.createFilter("(?xs).*line.*", null, false, Filter.Result.DENY, Filter.Result.ACCEPT);
8585
final Result singleLineResult = filter.filter(null, null, null, (Object) singleLine, null);
8686
final Result multiLineResult = filter.filter(null, null, null, (Object) multiLine, null);
8787
assertThat(singleLineResult, equalTo(Result.DENY));
@@ -190,22 +190,17 @@ void testRegexFilterGetters() {
190190
*/
191191
@Test
192192
void testBuilderWithoutRegexNotValid() {
193-
194193
final RegexFilter.Builder builder = RegexFilter.newBuilder();
195-
196-
assertNull(builder.build());
194+
assertThrows(IllegalArgumentException.class, builder::build);
197195
}
198196

199197
/**
200198
* A builder with an invalid 'regex' expression should return null on 'build()'.
201199
*/
202200
@Test
203201
void testBuilderWithInvalidRegexNotValid() {
204-
205202
final RegexFilter.Builder builder = RegexFilter.newBuilder();
206-
207203
builder.setRegex("[a-z");
208-
209-
assertNull(builder.build());
204+
assertThrows(IllegalArgumentException.class, builder::build);
210205
}
211206
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ public abstract static class AbstractFilterBuilder<B extends AbstractFilterBuild
4949
* The action to perform when a match occurs.
5050
*/
5151
@PluginBuilderAttribute(ATTR_ON_MATCH)
52-
protected Result onMatch = Result.NEUTRAL;
52+
private Result onMatch = Result.NEUTRAL;
5353

5454
/**
5555
* The action to perform when a mismatch occurs.
5656
*/
5757
@PluginBuilderAttribute(ATTR_ON_MISMATCH)
58-
protected Result onMismatch = Result.DENY;
58+
private Result onMismatch = Result.DENY;
5959

6060
/**
6161
* Returns the action to apply when a match occurs
@@ -130,11 +130,10 @@ protected AbstractFilter(final Result onMatch, final Result onMismatch) {
130130
* Constructs a new instance configured by the given builder
131131
* @param builder the builder
132132
* @throws NullPointerException if the builder argument is {@code null}
133+
* @since 2.27.0
133134
*/
134135
protected AbstractFilter(final AbstractFilterBuilder<?> builder) {
135-
136-
Objects.requireNonNull(builder, "The 'builder' argument cannot be null.");
137-
136+
Objects.requireNonNull(builder, "builder");
138137
this.onMatch = Optional.ofNullable(builder.onMatch).orElse(Result.NEUTRAL);
139138
this.onMismatch = Optional.ofNullable(builder.onMismatch).orElse(Result.DENY);
140139
}

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

Lines changed: 28 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
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;
@@ -31,7 +28,6 @@
3128
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
3229
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
3330
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
34-
import org.apache.logging.log4j.core.config.plugins.PluginElement;
3531
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
3632
import org.apache.logging.log4j.core.util.Assert;
3733
import 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
}

log4j-core/src/main/java/org/apache/logging/log4j/core/filter/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* {@link org.apache.logging.log4j.core.Filter#ELEMENT_TYPE filter}.
2323
*/
2424
@Export
25-
@Version("2.25.3")
25+
@Version("2.27.0")
2626
package org.apache.logging.log4j.core.filter;
2727

2828
import org.osgi.annotation.bundle.Export;

0 commit comments

Comments
 (0)