Skip to content

Commit c48a0d2

Browse files
committed
Add improved validation in StringMatchFilter for null/empty text
1 parent 70f058d commit c48a0d2

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.apache.logging.log4j.core.config.plugins.Plugin;
2626
import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
2727
import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
28+
import org.apache.logging.log4j.core.config.plugins.validation.constraints.Required;
29+
import org.apache.logging.log4j.core.util.Assert;
2830
import org.apache.logging.log4j.message.Message;
2931
import org.apache.logging.log4j.util.PerformanceSensitive;
3032

@@ -41,7 +43,7 @@ public final class StringMatchFilter extends AbstractFilter {
4143

4244
private StringMatchFilter(final String text, final Result onMatch, final Result onMismatch) {
4345
super(onMatch, onMismatch);
44-
this.text = text;
46+
this.text = Assert.requireNonEmpty(text, "text");
4547
}
4648

4749
@Override
@@ -235,21 +237,26 @@ public static StringMatchFilter.Builder newBuilder() {
235237

236238
public static class Builder extends AbstractFilterBuilder<StringMatchFilter.Builder>
237239
implements org.apache.logging.log4j.core.util.Builder<StringMatchFilter> {
240+
238241
@PluginBuilderAttribute
239-
private String text = "";
242+
@Required(message = "No text provided for StringMatchFilter")
243+
private String text;
240244

241245
/**
242246
* Sets the text to search in event messages.
243247
* @param text the text to search in event messages.
244248
* @return this instance.
245249
*/
246250
public StringMatchFilter.Builder setMatchString(final String text) {
247-
this.text = text;
251+
this.text = Assert.requireNonEmpty(text, "The 'text' argument must not be null or empty.");
248252
return this;
249253
}
250254

251255
@Override
252256
public StringMatchFilter build() {
257+
if (!isValid()) {
258+
return null;
259+
}
253260
return new StringMatchFilter(this.text, this.getOnMatch(), this.getOnMismatch());
254261
}
255262
}

0 commit comments

Comments
 (0)