Skip to content

Commit dd7b6d3

Browse files
committed
Updates per PR Code Review (#3086)
+ made AbstractFiltter.AbstractFilterBuilder onMatch/onMismatch fields protected + added AbstractFilter(AbstractFilterBuilder) constructor + added RegexFilter.Builder implementation + added RegexFilter(Builder) constructor + moved RegexFilter Pattern compile into constructor + added fields to persist configuration propertties + getters (regexExpression, patternFlags) + changed private constructor to accept builder as argument + renamed private method 'targetMessageTest' to more approprriate 'getMessageTextByType' + added Javadoc + grouped deprecations
1 parent f0ec0b8 commit dd7b6d3

File tree

5 files changed

+328
-78
lines changed

5 files changed

+328
-78
lines changed

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

+8-3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.hamcrest.CoreMatchers.equalTo;
2020
import static org.hamcrest.MatcherAssert.assertThat;
2121
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2223
import static org.junit.jupiter.api.Assertions.assertNull;
2324
import static org.junit.jupiter.api.Assertions.assertSame;
2425
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -43,13 +44,17 @@ static void before() {
4344
@Test
4445
void testRegexFilterDoesNotThrowWithAllTheParametersExceptRegexEqualNull() {
4546
assertDoesNotThrow(() -> {
46-
RegexFilter.createFilter(".* test .*", null, null, null, null);
47+
RegexFilter.newBuilder().setRegex(".* test .*").build();
4748
});
4849
}
4950

5051
@Test
5152
void testThresholds() throws Exception {
52-
RegexFilter filter = RegexFilter.createFilter(".* test .*", null, false, null, null);
53+
RegexFilter filter = RegexFilter.newBuilder()
54+
.setRegex(".* test .*")
55+
.setUseRawMsg(false)
56+
.build();
57+
assertNotNull(filter);
5358
filter.start();
5459
assertTrue(filter.isStarted());
5560
assertSame(
@@ -65,7 +70,7 @@ void testThresholds() throws Exception {
6570
.setMessage(new SimpleMessage("test")) //
6671
.build();
6772
assertSame(Filter.Result.DENY, filter.filter(event));
68-
filter = RegexFilter.createFilter(null, null, false, null, null);
73+
filter = RegexFilter.newBuilder().build();
6974
assertNull(filter);
7075
}
7176

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

+31-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.logging.log4j.core.filter;
1818

19+
import java.util.Objects;
20+
import java.util.Optional;
1921
import org.apache.logging.log4j.Level;
2022
import org.apache.logging.log4j.Marker;
2123
import org.apache.logging.log4j.core.AbstractLifeCycle;
@@ -43,16 +45,30 @@ public abstract static class AbstractFilterBuilder<B extends AbstractFilterBuild
4345
public static final String ATTR_ON_MISMATCH = "onMismatch";
4446
public static final String ATTR_ON_MATCH = "onMatch";
4547

48+
/**
49+
* The action to perform when a match occurs.
50+
*/
4651
@PluginBuilderAttribute(ATTR_ON_MATCH)
47-
private Result onMatch = Result.NEUTRAL;
52+
protected Result onMatch = Result.NEUTRAL;
4853

54+
/**
55+
* The action to perform when a mismatch occurs.
56+
*/
4957
@PluginBuilderAttribute(ATTR_ON_MISMATCH)
50-
private Result onMismatch = Result.DENY;
58+
protected Result onMismatch = Result.DENY;
5159

60+
/**
61+
* Returns the action to apply when a match occurs
62+
* @return the match result
63+
*/
5264
public Result getOnMatch() {
5365
return onMatch;
5466
}
5567

68+
/**
69+
* Returns the action to apply when a mismatch occurs
70+
* @return the mismatch result
71+
*/
5672
public Result getOnMismatch() {
5773
return onMismatch;
5874
}
@@ -110,6 +126,19 @@ protected AbstractFilter(final Result onMatch, final Result onMismatch) {
110126
this.onMismatch = onMismatch == null ? Result.DENY : onMismatch;
111127
}
112128

129+
/**
130+
* Constructs a new instance configured by the given builder
131+
* @param builder the builder
132+
* @throws NullPointerException if the builder argument is {@code null}
133+
*/
134+
protected AbstractFilter(final AbstractFilterBuilder<?> builder) {
135+
136+
Objects.requireNonNull(builder, "The 'builder' argument cannot be null.");
137+
138+
this.onMatch = Optional.ofNullable(builder.onMatch).orElse(Result.NEUTRAL);
139+
this.onMismatch = Optional.ofNullable(builder.onMismatch).orElse(Result.DENY);
140+
}
141+
113142
@Override
114143
protected boolean equalsImpl(final Object obj) {
115144
if (this == obj) {

0 commit comments

Comments
 (0)