Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update StringMatchFilter builder API (#3509) #3510

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

JWT007
Copy link
Contributor

@JWT007 JWT007 commented Mar 2, 2025

#3509

Fix StringMatchFilter Validation

  • Guard against NPEs
  • added private constructor to builder
  • added getText() accessor
  • add JVerify nullability annotations
  • added detailed javadoc with implementation details
  • optimized AbstractLifeCycle and AbstractFilter "equalsImpl" implementations
  • added changelog

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
* Guard against NPEs
* added private constructor to builder
* added getText() accessor
* add JVerify nullability annotations
* added detailed javadoc with implementation details
* optimized AbstractLifeCycle and AbstractFilter "equalsImpl" implementations
* added changelog
@JWT007 JWT007 requested a review from ppkarwasz March 2, 2025 12:23
@JWT007 JWT007 self-assigned this Mar 2, 2025
Copy link

github-actions bot commented Mar 2, 2025

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

@JWT007
Copy link
Contributor Author

JWT007 commented Mar 2, 2025

NOTE: This PR replaces the 2.x PR #3158 (#3153) for the main 3.x branch.
A cherry pick was not possible due to missing API.

JWT007 added 3 commits March 2, 2025 14:44
@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
Copy link
Contributor

@ppkarwasz ppkarwasz left a comment

Choose a reason for hiding this comment

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

My 2 cents:

})
void testFilterBuilderFailsWithExceptionOnNullText() {
StringMatchFilter.Builder stringMatchFilterBuilder = StringMatchFilter.newBuilder();
Assertions.assertThrows(IllegalArgumentException.class, () -> stringMatchFilterBuilder.setText(null));
Copy link
Contributor

Choose a reason for hiding this comment

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

This is certainly debatable, but I would rather expect NPE to be thrown here.

The way I see it, there are two levels of validation:

  • The parameter is annotated as @NonNull and we expect the programmer to check for that. In this case NPE seems appropriate.
  • There is a more involved validation process that requires a non-empty string. In this case IllegalArgumentException seems appropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is only because I used the Log4j Assert which throws IllegalArgumentException.

    public Builder setText(final String text) {
            this.text = Assert.requireNonEmpty(text, "The 'text' argument must not be null or empty.");
            return this;
        }

I could of course add an extra Objects.requiresNonNull if you think that is better?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we can modify Assert.requireNonEmpty? What do you think?

It is mostly a question of convention (and we have none), but I think that it is worth differentiating the null and empty cases, since there are plenty of tools that do static analysis on nulls and the caller could use one.

Comment on lines +99 to +102
void testConfigurationWithTextNEG(final Configuration configuration) {
final Filter filter = configuration.getFilter();
assertNull(filter, "The filter should be null.");
}
Copy link
Contributor

Choose a reason for hiding this comment

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

This would rather belong in a unit test for PluginBuilder or similar.
The fact that the user misspells StringMatchFilter or another name, doesn't seem relevant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this simply tests that a misconfigured (or not at all) fails on build and returns null I think.

A "" invalid name would throw in an entirely different locattion I think - unresolved plugin type.

Copy link
Contributor

Choose a reason for hiding this comment

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

Unless I am mistaken this tests that a misspelled plugin name (StringMatchFfilter) will be ignored.

Since the plugin name is misspelled PluginBuilder will not find the plugin and return null. No code in StringMatchFilter.java will be executed.

Comment on lines -55 to +89
public Result filter(
final Logger logger, final Level level, final Marker marker, final Object msg, final Throwable t) {
return filter(logger.getMessageFactory().newMessage(msg).getFormattedMessage());
public Result filter(final LogEvent event) {
Objects.requireNonNull(event, "The 'event' argument must not be null.");
return filter(event.getMessage().getFormattedMessage());
Copy link
Contributor

Choose a reason for hiding this comment

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

Please don't sort methods. It makes it much harder to review the code.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed with @ppkarwasz. Please strive to avoid cosmetic changes on the existing code in PRs. Feel free to submit a follow-up PR/commit for these kind of changes.

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
None yet
Development

Successfully merging this pull request may close these issues.

Log4j 3.x - StringMatchFilter - guard potential NPEs and builder validation/API
3 participants