Class ExpressionFilter

java.lang.Object
org.apache.log4j.spi.Filter
org.apache.log4j.filter.ExpressionFilter
All Implemented Interfaces:
org.apache.log4j.spi.OptionHandler

public class ExpressionFilter extends org.apache.log4j.spi.Filter
A filter supporting complex expressions - supports both infix and postfix expressions (infix expressions must first be converted to postfix prior to processing).

See org.apache.log4j.chainsaw.LoggingEventFieldResolver.java for the correct names for logging event fields used when building expressions.

See org.apache.log4j.chainsaw.rule package for a list of available rules which can be applied using the expression syntax.

See org.apache.log4j.chainsaw.RuleFactory for the symbols used to activate the corresponding rules.

NOTE: Grouping using parentheses is supported - all tokens must be separated by spaces, and operands which contain spaces are not yet supported.

Example:

In order to build a filter that displays all messages with infomsg-45 or infomsg-44 in the message, as well as all messages with a level of WARN or higher, build an expression using the LikeRule (supports java.util.regex based regular expressions) and the InequalityRule. ( MSG LIKE infomsg-4[4,5] ) && ( LEVEL >= WARN )

Three options are required: Expression - the expression to match ConvertInFixToPostFix - convert from infix to posfix (default true) AcceptOnMatch - true or false (default true)

Meaning of AcceptToMatch: If there is a match between the value of the Expression option and the LoggingEvent and AcceptOnMatch is true, the decide(org.apache.log4j.spi.LoggingEvent) method returns Filter.ACCEPT.

If there is a match between the value of the Expression option and the LoggingEvent and AcceptOnMatch is false, Filter.DENY is returned.

If there is no match, Filter.NEUTRAL is returned.

  • Field Details

    • acceptOnMatch

      boolean acceptOnMatch
      accept on match.
    • convertInFixToPostFix

      boolean convertInFixToPostFix
      Convert in-fix to post-fix.
    • expression

      String expression
      Expression.
    • expressionRule

      Rule expressionRule
      Evaluated rule.
  • Constructor Details

    • ExpressionFilter

      public ExpressionFilter()
  • Method Details

    • activateOptions

      public void activateOptions()
      Specified by:
      activateOptions in interface org.apache.log4j.spi.OptionHandler
      Overrides:
      activateOptions in class org.apache.log4j.spi.Filter
    • setExpression

      public void setExpression(String exp)
      Set exp.
      Parameters:
      exp - exp.
    • getExpression

      public String getExpression()
      Get expression.
      Returns:
      expression.
    • setConvertInFixToPostFix

      public void setConvertInFixToPostFix(boolean newValue)
      Set convert in-fix to post-fix.
      Parameters:
      newValue - new value.
    • getConvertInFixToPostFix

      public boolean getConvertInFixToPostFix()
      Get in-fix to post-fix conversion setting.
      Returns:
      true if in-fix expressions are converted to post-fix.
    • setAcceptOnMatch

      public void setAcceptOnMatch(boolean newValue)
      Set whether filter should accept events if they match the expression.
      Parameters:
      newValue - if true, accept on match.
    • getAcceptOnMatch

      public boolean getAcceptOnMatch()
      Gets whether filter accepts matching or non-matching events.
      Returns:
      if true, accept matching events.
    • decide

      public int decide(org.apache.log4j.spi.LoggingEvent event)
      Determines if event matches the filter.
      Specified by:
      decide in class org.apache.log4j.spi.Filter
      Parameters:
      event - logging event;
      Returns:
      Filter.NEUTRAL is there is no string match.