Skip to content

Removed @PluginAttribute 'patternFlags' argument from @PluginFactory for RegexFilter. (#3086)#3512

Closed
JWT007 wants to merge 7 commits into
apache:2.xfrom
JWT007:bugfix/LOG4J-3086-2x
Closed

Removed @PluginAttribute 'patternFlags' argument from @PluginFactory for RegexFilter. (#3086)#3512
JWT007 wants to merge 7 commits into
apache:2.xfrom
JWT007:bugfix/LOG4J-3086-2x

Conversation

@JWT007

@JWT007 JWT007 commented Mar 2, 2025

Copy link
Copy Markdown
Contributor

(#3086)

Removed @PluginAttribute 'patternFlags' argument from https://github.com/pluginfactory for RegexFilter.

Actually created new https://github.com/pluginfactory without the argument and deprecated old 'createFilter' method.

See ticket for discussion of the reasoning - short version - pattern flags can be passed as part of RegEx expression.

Also guarded the "PatternCompile" method against excepttions - logging and returning null if the pattern fails to compile.

Recreated from 2.x and superceding PR #3463

jethomas-tsi and others added 7 commits March 2, 2025 21:11
+ 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
+ added validation checks to RegexFilter
+ added JVerify nullability annotations to RegexFilter
+ updated javadoc
+ replaced deprecated usages of CompositeFilter#getFilters with CompositeFilter#getFiltersArray in AbstractFilterableTest
@JWT007

JWT007 commented Mar 2, 2025

Copy link
Copy Markdown
Contributor Author

New branch based on 2.x branch - replacing PR #3463

@JWT007 JWT007 requested a review from ppkarwasz March 2, 2025 20:28
@JWT007 JWT007 self-assigned this Mar 2, 2025
@JWT007 JWT007 added bug Incorrect, unexpected, or unintended behavior of existing code java Pull requests that update Java code configuration Affects the configuration system in a general way labels Mar 2, 2025
@JWT007 JWT007 linked an issue Mar 2, 2025 that may be closed by this pull request
@github-actions

github-actions Bot commented Mar 2, 2025

Copy link
Copy Markdown
Contributor
Job Requested goals Build Tool Version Build Outcome Build Scan®
build-macos-latest clean install 3.9.8 Build Scan PUBLISHED
build-ubuntu-latest clean install 3.9.8 Build Scan PUBLISHED
build-windows-latest clean install 3.9.8 Build Scan PUBLISHED
Generated by gradle/develocity-actions

@vy vy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for the hard work @JWT007! Dropped some remarks.

public Result filter(final LogEvent event) {
final String text = targetMessageTest(event.getMessage());
return filter(text);
Objects.requireNonNull(event, "The 'event' argument must not be null.");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Objects.requireNonNull(event, "The 'event' argument must not be null.");
Objects.requireNonNull(event, "event");

* The regular expression to match.
*/
@PluginBuilderAttribute
@Required(message = "No 'regex' provided for RegexFilter")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't provide a failure message for cases where the failure system should be providing the necessary context.

Suggested change
@Required(message = "No 'regex' provided for RegexFilter")
@Required

/** {@inheritDoc} */
@Override
public boolean isValid() {
return (Strings.isNotEmpty(this.regex));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (Strings.isNotEmpty(this.regex));
return Strings.isNotEmpty(this.regex);

Comment on lines +323 to 338
public @Nullable RegexFilter build() {

// validate the "regex" attribute
if (Strings.isEmpty(this.regex)) {
LOGGER.error("Unable to create RegexFilter: The 'regex' attribute be set to a non-empty String.");
return null;
}

// build with *safety* to not throw exceptions
try {
return new RegexFilter(this);
} catch (final Exception ex) {
LOGGER.error("Unable to create RegexFilter. {}", ex.getMessage(), ex);
return null;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had a dev@ discussion about this, and the conclusion is to throw exception on failures while building components. Would you mind updating this build() logic accordingly, please?

* @deprecated pattern flags have been deprecated - they can just be included in the regex-expression.
*/
@Deprecated
private static final int DEFAULT_PATTERN_FLAGS = 0;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIU, this constant is only used in toPatternFlags(), could you move it into that method, please?

Comment on lines +389 to 392
@Deprecated
public String[] getPatternFlags() {
return this.patternFlags.clone();
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're adding a new deprecated method? Why don't we simply not add it?

Boolean.TRUE.equals(useRawMsg), Pattern.compile(regex, toPatternFlags(patternFlags)), match, mismatch);

// LOG4J-3086 - pattern-flags can be embedded in RegEx expression
Objects.requireNonNull(regex, "The 'regex' argument must not be null.");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Objects.requireNonNull(regex, "The 'regex' argument must not be null.");
Objects.requireNonNull(regex, "regex");

@ppkarwasz ppkarwasz added this to the 2.25.0 milestone Apr 13, 2025
*/
@PluginBuilderAttribute(ATTR_ON_MATCH)
private Result onMatch = Result.NEUTRAL;
protected Result onMatch = Result.NEUTRAL;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a getOnMatch(), no need to expose the field too.

*/
@PluginBuilderAttribute(ATTR_ON_MISMATCH)
private Result onMismatch = Result.DENY;
protected Result onMismatch = Result.DENY;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is already a getOnMismatch(), no need to expose the field too.

@ppkarwasz ppkarwasz modified the milestones: 2.25.0, 2.25.1 May 25, 2025
@ppkarwasz ppkarwasz removed this from the 2.25.1 milestone Jun 21, 2025
vy added a commit to ramanathan1504/logging-log4j2 that referenced this pull request Jun 11, 2026
@vy

vy commented Jun 11, 2026

Copy link
Copy Markdown
Member

I've resurrected this in #4119. Thanks so much for the ground work @JWT007, and apologies for the late processing.

@vy vy closed this Jun 11, 2026
@github-project-automation github-project-automation Bot moved this from To triage to Done in Log4j bug tracker Jun 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Incorrect, unexpected, or unintended behavior of existing code configuration Affects the configuration system in a general way java Pull requests that update Java code

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Fix RegexFilter pattern flags

4 participants