Skip to content

Commit cdd9eed

Browse files
committed
Updates per PR Code Review (apache#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 e8bbd1d commit cdd9eed

File tree

5 files changed

+342
-74
lines changed

5 files changed

+342
-74
lines changed

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

+16-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static org.hamcrest.CoreMatchers.equalTo;
2020
import static org.hamcrest.MatcherAssert.assertThat;
21+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
22+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2123
import static org.junit.jupiter.api.Assertions.assertNull;
2224
import static org.junit.jupiter.api.Assertions.assertSame;
2325
import static org.junit.jupiter.api.Assertions.assertTrue;
@@ -40,8 +42,19 @@ public static void before() {
4042
}
4143

4244
@Test
43-
public void testThresholds() throws Exception {
44-
RegexFilter filter = RegexFilter.createFilter(".* test .*", null, false, null, null);
45+
void testRegexFilterDoesNotThrowWithAllTheParametersExceptRegexEqualNull() {
46+
assertDoesNotThrow(() -> {
47+
RegexFilter.newBuilder().setRegex(".* test .*").build();
48+
});
49+
}
50+
51+
@Test
52+
void testThresholds() throws Exception {
53+
RegexFilter filter = RegexFilter.newBuilder()
54+
.setRegex(".* test .*")
55+
.setUseRawMsg(false)
56+
.build();
57+
assertNotNull(filter);
4558
filter.start();
4659
assertTrue(filter.isStarted());
4760
assertSame(
@@ -59,7 +72,7 @@ public void testThresholds() throws Exception {
5972
.setMessage(new SimpleMessage("test")) //
6073
.build();
6174
assertSame(Filter.Result.DENY, filter.filter(event));
62-
filter = RegexFilter.createFilter(null, null, false, null, null);
75+
filter = RegexFilter.newBuilder().build();
6376
assertNull(filter);
6477
}
6578

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)