sdeboy      2003/10/27 00:35:31

  Modified:    src/java/org/apache/log4j/chainsaw
                        LoggingEventFieldResolver.java
  Added:       src/java/org/apache/log4j/varia ExpressionFilter.java
  Log:
  First commit of ExpressionRule-based filter - see the org.apache.log4j.chainsaw.rule 
package and org.apache.log4j.chainsaw.LoggingEventFieldResolver for more information.
  
  Supported operators: !, ==, !=, ~= (partial text), like (ORO), <, <=, >, >=, ||, &&
  
  Supported event keywords:
  
  LOGGER
  LEVEL
  CLASS
  FILE
  LINE
  METHOD
  MSG
  NDC
  EXCEPTION
  TIMESTAMP
  THREAD
  MDC.*
  PROP.*
  
  NOTE:  MDC and Properties require a user-specified parameter to do the lookup in the 
table - specified after the '.' - for example, MDC.userID
  
  Also supports grouping via parentheses.
  
  Revision  Changes    Path
  1.1                  
jakarta-log4j/src/java/org/apache/log4j/varia/ExpressionFilter.java
  
  Index: ExpressionFilter.java
  ===================================================================
  /*
   * ============================================================================
   *                   The Apache Software License, Version 1.1
   * ============================================================================
   *
   *    Copyright (C) 1999 The Apache Software Foundation. All rights reserved.
   *
   * Redistribution and use in source and binary forms, with or without modifica-
   * tion, are permitted provided that the following conditions are met:
   *
   * 1. Redistributions of  source code must  retain the above copyright  notice,
   *    this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright notice,
   *    this list of conditions and the following disclaimer in the documentation
   *    and/or other materials provided with the distribution.
   *
   * 3. The end-user documentation included with the redistribution, if any, must
   *    include  the following  acknowledgment:  "This product includes  software
   *    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
   *    Alternately, this  acknowledgment may  appear in the software itself,  if
   *    and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "log4j" and  "Apache Software Foundation"  must not be used to
   *    endorse  or promote  products derived  from this  software without  prior
   *    written permission. For written permission, please contact
   *    [EMAIL PROTECTED]
   *
   * 5. Products  derived from this software may not  be called "Apache", nor may
   *    "Apache" appear  in their name,  without prior written permission  of the
   *    Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
   * APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
   * DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
   * OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
   * ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
   * (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * This software  consists of voluntary contributions made  by many individuals
   * on  behalf of the Apache Software  Foundation.  For more  information on the
   * Apache Software Foundation, please see <http://www.apache.org/>.
   *
   */
  
  package org.apache.log4j.varia;
  
  import org.apache.log4j.chainsaw.rule.ExpressionRule;
  import org.apache.log4j.chainsaw.rule.Rule;
  import org.apache.log4j.spi.Filter;
  import org.apache.log4j.spi.LoggingEvent;
  
  
  /**
   *A filter supporting complex expressions - supports both infix and postfix 
expressions 
   *(infix expressions must first be converted to postfix prior to processing).
   *
   *See <code>org.apache.log4j.chainsaw.LoggingEventFieldResolver.java</code> 
   *for the correct names for logging event fields used when building expressions.
   *
   *See <org.apache.log4j.chainsaw.rule</code> package for a list of available rules 
which can
   *be applied using the expression syntax.
   *
   *See <code>org.apache.log4j.chainsaw.RuleFactory</code> 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 ORO-based regular expressions) and the InequalityRule. 
   * <b> ( MSG LIKE infomsg-4[4,5] ) && ( LEVEL >= WARN ) </b>
   *  
   *Three options are required:
   *  <b>Expression</b> - the expression to match
   *  <b>ConvertInFixToPostFix</b> - convert from infix to posfix (default true)
   *  <b>AcceptOnMatch</b> - true or false (default true)
   *
   * Meaning of <b>AcceptToMatch</b>:
   * If there is a match between the value of the
   * Expression option and the [EMAIL PROTECTED] LoggingEvent} and AcceptOnMatch is 
true,
   * the [EMAIL PROTECTED] #decide} method returns [EMAIL PROTECTED] Filter#ACCEPT}.
   *
   * If there is a match between the value of the
   * Expression option and the [EMAIL PROTECTED] LoggingEvent} and AcceptOnMatch is 
false,
   * [EMAIL PROTECTED] Filter#DENY} is returned.
   *
   * If there is no match, [EMAIL PROTECTED] Filter#NEUTRAL} is returned.
   *
   * @author Scott Deboy [EMAIL PROTECTED]
   */
  public class ExpressionFilter extends Filter {
    boolean acceptOnMatch = true;
    boolean convertInFixToPostFix = true;
    String expression;
    Rule expressionRule;
  
    public void activateOptions() {
      expressionRule =
        ExpressionRule.getRule(expression, !convertInFixToPostFix);
    }
  
    public void setExpression(String expression) {
      this.expression = expression;
    }
  
    public String getExpression() {
      return expression;
    }
  
    public void setConvertInFixToPostFix(boolean convertInFixToPostFix) {
      this.convertInFixToPostFix = convertInFixToPostFix;
    }
  
    public boolean getConvertInFixToPostFix() {
      return convertInFixToPostFix;
    }
  
    public void setAcceptOnMatch(boolean acceptOnMatch) {
      this.acceptOnMatch = acceptOnMatch;
    }
  
    public boolean getAcceptOnMatch() {
      return acceptOnMatch;
    }
  
    /**
       Returns [EMAIL PROTECTED] Filter#NEUTRAL} is there is no string match.
     */
    public int decide(LoggingEvent event) {
      if ((expression == null)) {
        return Filter.NEUTRAL;
      }
  
      if (expressionRule.evaluate(event)) {
        if (acceptOnMatch) {
          return Filter.ACCEPT;
        } else {
          return Filter.DENY;
        }
      } else {
        return Filter.NEUTRAL;
      }
    }
  }
  
  
  
  1.8       +5 -6      
jakarta-log4j/src/java/org/apache/log4j/chainsaw/LoggingEventFieldResolver.java
  
  Index: LoggingEventFieldResolver.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/LoggingEventFieldResolver.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- LoggingEventFieldResolver.java    27 Oct 2003 08:22:15 -0000      1.7
  +++ LoggingEventFieldResolver.java    27 Oct 2003 08:35:31 -0000      1.8
  @@ -87,9 +87,8 @@
    * NOTE:  the values for the 'keyName' portion of the MDC and PROP mappings must
    * be an exact match to the key in the hashTable (case sensitive).
    *
  - * If the passed-in field is null, an empty string is returned.
  - * If the passed-in field doesn't match an entry in the above-described
  - * mapping, the passed-in field is returned.
  + * If the passed-in field is null or doesn't match an entry in the above-described
  + * mapping, an exception is thrown.
    *
    * @author Scott Deboy <[EMAIL PROTECTED]>
    * @author Paul Smith <[EMAIL PROTECTED]>
  @@ -144,7 +143,7 @@
   
     public Object getValue(String fieldName, LoggingEvent event) {
       if (fieldName == null) {
  -      return EMPTY_STRING;
  +        throw new RuntimeException("null field name");
       }
   
       String upperField = fieldName.toUpperCase();
  @@ -177,12 +176,12 @@
         //note: need to use actual fieldname since case matters
         Object mdcValue = event.getMDC(fieldName.substring(4));
   
  -      return ((mdcValue == null) ? "" : mdcValue.toString());
  +      return ((mdcValue == null) ? EMPTY_STRING : mdcValue.toString());
       } else if (upperField.startsWith(PROP_FIELD)) {
         //note: need to use actual fieldname since case matters
         String propValue = event.getProperty(fieldName.substring(5));
   
  -      return ((propValue == null) ? "" : propValue);
  +      return ((propValue == null) ? EMPTY_STRING : propValue);
       }
   
       //there wasn't a match, so throw a runtime exception
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to