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

Fix RegexFilter pattern flags #3086

Open
JWT007 opened this issue Oct 14, 2024 · 2 comments · May be fixed by #3512 or #3513
Open

Fix RegexFilter pattern flags #3086

JWT007 opened this issue Oct 14, 2024 · 2 comments · May be fixed by #3512 or #3513
Assignees
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

Comments

@JWT007
Copy link
Contributor

JWT007 commented Oct 14, 2024

In the Log4j 2.x documentation for the RegexFilter:

https://logging.apache.org/log4j/2.x/manual/filters.html#RegexFilter

The only two configuration attributes are "regex" and "useRawMsg" (and the common filter attributes onMatch/onMismatch).

In the RegExFilter @pluginfactory there is a nested element for a set of Strings which is not mentioned in the documentation:

@PluginFactory
    public static RegexFilter createFilter(
            // @formatter:off
            @PluginAttribute("regex") final String regex,
            @PluginElement("PatternFlags") final String[] patternFlags,
            @PluginAttribute("useRawMsg") final Boolean useRawMsg,
            @PluginAttribute("onMatch") final Result match,
            @PluginAttribute("onMismatch") final Result mismatch)
            // @formatter:on
            throws IllegalArgumentException, IllegalAccessException {

If I understand correctly one can provide field names from the Pattern class here: (CASE_INSENSITIVE, COMMENTS, etc.).

The example in the Plugin Reference for the RegexFilter shows an example of nested strings but is not very clear with "a-String-implementation":

<RegexFilter onMatch=""
             onMismatch=""
             regex=""
             useRawMsg="">
    <a-String-implementation/><!-- multiple occurrences allowed -->
</RegexFilter>

Is it?:

<RegExFilter ...>
  <PatternFlags>CASE_INSENTIVE</PatternFlags>
  <PatternFlags>COMMENTS</PatternFlags>
</RegExFilter>

A usage example would be helpful.

Maybe this API is not really "public"? In the log4j-core-test RegexFilter test it seems the RegexFilter is never tested with anything but a null value for the patternFlags.

@ppkarwasz ppkarwasz changed the title Log4j 2.x Documentation - RegexFilter does not document the possible nested element PatternFlags Fix RegexFilter pattern flags Oct 17, 2024
@ppkarwasz
Copy link
Contributor

Hi @JWT007,

Thank you for helping us checking our documentation against the automatically-generated plugin reference. We created the plugin reference exactly for this purpose and we are happy you find it useful.

This is a longstanding LOG4J2-696 bug, old as Log4j 2 itself. The attempted solution has no chance to work, since the @PluginElement annotation only accepts classes annotated with @Plugin and String is final.

Since you can embed pattern flags in the regex (e.g. (?i) instead of CASE_SENSITIVE), I believe that we can just remove the annotation on the parameter.

@ppkarwasz ppkarwasz added bug Incorrect, unexpected, or unintended behavior of existing code and removed waiting-for-maintainer labels Oct 17, 2024
JWT007 pushed a commit to JWT007/logging-log4j2 that referenced this issue Feb 12, 2025
@JWT007
Copy link
Contributor Author

JWT007 commented Feb 12, 2025

@ppkarwasz - added another new PR.

Please review if OK how I deprecated old pluginFactory and created new one.

JWT007 pushed a commit to JWT007/logging-log4j2 that referenced this issue Feb 17, 2025
+ 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
JWT007 pushed a commit to JWT007/logging-log4j2 that referenced this issue Feb 17, 2025
JWT007 pushed a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 pushed a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
+ 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
JWT007 pushed a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
+ 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 added the java Pull requests that update Java code label Mar 2, 2025
@JWT007 JWT007 self-assigned this Mar 2, 2025
@JWT007 JWT007 added the configuration Affects the configuration system in a general way label Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 pushed a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 pushed a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
+ 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
JWT007 pushed a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
+ 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 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
+ 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
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
+ 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 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
JWT007 added a commit to JWT007/logging-log4j2 that referenced this issue Mar 2, 2025
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
2 participants