ceki        01/09/03 15:37:30

  Added:       src/java/org/apache/log4j/varia LevelMatchFilter.java
  Log:
  In replacement of PriorityMatchFilter.java
  
  Revision  Changes    Path
  1.1                  
jakarta-log4j/src/java/org/apache/log4j/varia/LevelMatchFilter.java
  
  Index: LevelMatchFilter.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included  with this distribution in
   * the LICENSE.APL file.
   */
  
  package org.apache.log4j.varia;
  
  import org.apache.log4j.Level;
  import org.apache.log4j.spi.Filter;
  import org.apache.log4j.spi.LoggingEvent;
  import org.apache.log4j.helpers.OptionConverter;
  
  /**
     This is a very simple filter based on level matching.
  
  
     <p>The filter admits two options <b>LevelToMatch</b> and
     <b>AcceptOnMatch</b>. If there is an exact match between the value
     of the LevelToMatch option and the level of the {@link
     LoggingEvent}, then the {@link #decide} method returns {@link
     Filter#ACCEPT} in case the <b>AcceptOnMatch</b> option value is set
     to <code>true</code>, if it is <code>false</code> then {@link
     Filter#DENY} is returned. If there is no match, {@link
     Filter#NEUTRAL} is returned.
  
     <p>See configuration files <a
     href="../xml/doc-files/test11.xml">test11.xml</a> and <a
     href="../xml/doc-files/test12.xml">test12.xml</a> for an example of
     seeting up a <code>LevelMatchFilter</code>.
  
     @author Ceki G&uuml;lc&uuml;
  
     @since 0.9.1 */
  public class LevelMatchFilter extends Filter {
    
    /**
       @deprecated Options are now handled using the JavaBeans paradigm.
       This constant is not longer needed and will be removed in the
       <em>near</em> term.
     */
    public static final String LEVEL_TO_MATCH_OPTION = "LevelToMatch";
  
    /**
       @deprecated Options are now handled using the JavaBeans paradigm.
       This constant is not longer needed and will be removed in the
       <em>near</em> term.
     */
    public static final String ACCEPT_ON_MATCH_OPTION = "AcceptOnMatch";
    
    /**
       Do we return ACCEPT when a match occurs. Default is
       <code>true</code>.  */
    boolean acceptOnMatch = true;
  
    /**
     */
    Level levelToMatch;
  
    /**
       @deprecated We now use JavaBeans introspection to configure
       components.
    */
    public
    String[] getOptionStrings() {
      return new String[] {LEVEL_TO_MATCH_OPTION, ACCEPT_ON_MATCH_OPTION};
    }
  
    /**
       @deprecated Use the setter method for the option directly instead
       of the generic <code>setOption</code> method. 
  
       @deprecated We now use JavaBeans introspection to configure
       components. 
    */
    public
    void setOption(String key, String value) {    
      if(key.equalsIgnoreCase(LEVEL_TO_MATCH_OPTION)) {
        levelToMatch = OptionConverter.toLevel(value, null);
      } else if (key.equalsIgnoreCase(ACCEPT_ON_MATCH_OPTION)) {
        acceptOnMatch = OptionConverter.toBoolean(value, acceptOnMatch);
      }
    }
    
    public
    void setLevelToMatch(String level) {
      levelToMatch = OptionConverter.toLevel(level, null);
    }
    
    public
    String getLevelToMatch() {
      return levelToMatch == null ? null : levelToMatch.toString();
    }
    
    public
    void setAcceptOnMatch(boolean acceptOnMatch) {
      this.acceptOnMatch = acceptOnMatch;
    }
    
    public
    boolean getAcceptOnMatch() {
      return acceptOnMatch;
    }
    
  
    /**
       Return the decision of this filter.
  
       Returns {@link Filter#NEUTRAL} if the <b>LevelToMatch</b>
       option is not set.  Otherwise, the returned decision is defined
       according to the following table:
  
       <p><table border=1>
       <tr><th rowspan="2" BGCOLOR="#AAAAFF">Did a level match occur?</th>
         <th colspan="2" BGCOLOR="#CCCCCC">AcceptOnMatch setting</th>
       
       <tr><td BGCOLOR="#CCCCCC" align="center">TRUE</td>
         <td BGCOLOR="#CCCCCC" align="center">FALSE</td>
       
       <tr><td BGCOLOR="#AAAAFF" align="center">TRUE</td>
         <td>{@link Filter#ACCEPT}</td><td>{@link Filter#DENY}</td>
       <tr><td BGCOLOR="#AAAAFF" align="center">FALSE</td>
         <td>{@link Filter#DENY}</td><td>{@link Filter#ACCEPT}</td>
       
         <caption align="bottom">Filter decision in function of whether a match
         occured and the AcceptOnMatch settings</caption> 
      </table> */
    public
    int decide(LoggingEvent event) {
      if(this.levelToMatch == null) {
        return Filter.NEUTRAL;
      }
      
      boolean matchOccured = false;
      if(this.levelToMatch == event.level) {
        matchOccured = true;
      }
  
      if(this.acceptOnMatch ^ matchOccured) {
        return Filter.DENY;
      } else {
        return Filter.ACCEPT;
      }
    }
  }
  
  
  

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

Reply via email to