logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Scott Deboy" <sde...@comotivsystems.com>
Subject RE: cvs commit: logging-log4j/src/java/org/apache/log4j/filter AndFilter.java
Date Sat, 08 Jan 2005 07:02:51 GMT
I thought I'd throw this filter out there for discussion.  Joran implicit actions are really
powerful once you figure out how to use them.

This filter should support composability and negation, but it's pretty complicated compared
to the expressionFilter alternative.

The only real benefit to using this filter over defining an AND-based expression and using
expressionFilter is for those folks who have written custom filters that trigger based on
internal state or some non-logging event stimulus.

The other reason to use this filter over an AND-based expression filter is if the expression
rule evaluation is dog slow and the filter evaluation was fast (hopefully this is no longer
the case).

It may not be what others were interested in, but it seems to work well and doesn't require
any framework changes.

I won't be offended if it's replaced/reverted, but I thought I'd start the ball rolling.

Scott


-----Original Message-----
From:	sdeboy@apache.org [mailto:sdeboy@apache.org]
Sent:	Fri 1/7/2005 10:57 PM
To:	logging-log4j-cvs@apache.org
Cc:	
Subject:	cvs commit: logging-log4j/src/java/org/apache/log4j/filter AndFilter.java
sdeboy      2005/01/07 22:57:48

  Added:       src/java/org/apache/log4j/filter AndFilter.java
  Log:
  Relying on Joran implicit actions to provide a filter that ANDs two nested filters.
  If both filters return accept, acceptOnMatch is used to return either ACCEPT or DENY
  
  Revision  Changes    Path
  1.1                  logging-log4j/src/java/org/apache/log4j/filter/AndFilter.java
  
  Index: AndFilter.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.log4j.filter;
  
  import org.apache.log4j.spi.Filter;
  import org.apache.log4j.spi.LoggingEvent;
  
  
  /**
   * A filter that 'and's the results of two filters together.
   * 
   * For the filter to process events, both contained filters must return Filter.ACCEPT.
   * 
   * If both filters do not return Filter.ACCEPT, Filter.NEUTRAL is returned.
   * 
   * If acceptOnMatch is set to true, Filter.ACCEPT is returned.
   * If acceptOnMatch is set to false, Filter.DENY is returned.
   * 
   * Here is an example config that will accept only events that contain BOTH
   * a DEBUG level AND 'test' in the message:
   * 
   *<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
   * <filter class="org.apache.log4j.filter.AndFilter">
   *  <filter1 class="org.apache.log4j.filter.LevelMatchFilter">
   *        <param name="levelToMatch" value="DEBUG" />
   *        <param name="acceptOnMatch" value="true" />
   *  </filter1>
   *  <filter2 class="org.apache.log4j.filter.StringMatchFilter">
   *        <param name="stringToMatch" value="test" />
   *        <param name="acceptOnMatch" value="true" />
   *  </filter2>
   *  <param name="acceptOnMatch" value="false"/>
   * </filter>
   * <filter class="org.apache.log4j.filter.DenyAllFilter"/>
   *<layout class="org.apache.log4j.SimpleLayout"/>
   *</appender>
   * 
   * To accept all events EXCEPT those events that contain a 
   * DEBUG level and 'test' in the message: 
   * change the AndFilter's acceptOnMatch param to false and remove the DenyAllFilter
   * 
   * NOTE: If you are defining a filter that is only relying on logging event content 
   * (no external or filter-managed state), you could opt instead
   * to use an ExpressionFilter with one of the following expressions:
   * 
   * LEVEL == DEBUG && MSG ~= 'test'
   * or
   * ! ( LEVEL == DEBUG && MSG ~= 'test' )
   *  
   * @author Scott Deboy sdeboy@apache.org
   */
  public class AndFilter extends Filter {
    Filter filter1;
    Filter filter2;
    boolean acceptOnMatch = true;
    
    public void activateOptions() {
      //nothing to do
    }
  
    public void setFilter1(Filter filter1) {
      System.out.println("filter 1 set to: " + filter1);
      this.filter1 = filter1;
    }
    
    public void setFilter2(Filter filter2) {
      System.out.println("filter 2 set to: " + filter2);
      this.filter2 = filter2;
    }
  
    public void setAcceptOnMatch(boolean acceptOnMatch) {
      this.acceptOnMatch = acceptOnMatch;
    }
    /**
     * If this event does not already contain location information, 
     * evaluate the event against the expression.
     * 
     * If the expression evaluates to true, generate a LocationInfo instance 
     * by creating an exception and set this LocationInfo on the event.
     * 
     * Returns {@link Filter#NEUTRAL}
     */
    public int decide(LoggingEvent event) {
      if ((Filter.ACCEPT == filter1.decide(event)) &&
          Filter.ACCEPT == filter2.decide(event)) {
        if(acceptOnMatch) {
          return Filter.ACCEPT;
        }
         return Filter.DENY;
      }
  //    System.out.println("neutral: " + event.getLevel() + ".." + event.getMessage()); 
      return Filter.NEUTRAL;
    }
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org





Mime
View raw message