LevelRangeFilter xref

View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17
    18  package org.apache.log4j.varia;
    19
    20  import org.apache.log4j.Level;
    21  import org.apache.log4j.spi.Filter;
    22  import org.apache.log4j.spi.LoggingEvent;
    23
    24  /**
    25     This is a very simple filter based on level matching, which can be
    26     used to reject messages with priorities outside a certain range.
    27
    28     <p>The filter admits three options <b>LevelMin</b>, <b>LevelMax</b>
    29     and <b>AcceptOnMatch</b>.
    30
    31     <p>If the level of the {@link LoggingEvent} is not between Min and Max
    32     (inclusive), then {@link Filter#DENY} is returned.
    33
    34     <p> If the Logging event level is within the specified range, then if
    35     <b>AcceptOnMatch</b> is true, {@link Filter#ACCEPT} is returned, and if
    36     <b>AcceptOnMatch</b> is false, {@link Filter#NEUTRAL} is returned.
    37
    38     <p>If <code>LevelMin</code>w is not defined, then there is no
    39     minimum acceptable level (ie a level is never rejected for
    40     being too "low"/unimportant).  If <code>LevelMax</code> is not
    41     defined, then there is no maximum acceptable level (ie a
    42     level is never rejected for beeing too "high"/important).
    43
    44     <p>Refer to the {@link
    45     org.apache.log4j.AppenderSkeleton#setThreshold setThreshold} method
    46     available to <code>all</code> appenders extending {@link
    47     org.apache.log4j.AppenderSkeleton} for a more convenient way to
    48     filter out events by level.
    49
    50     @author Simon Kitching
    51     @author based on code by Ceki G&uuml;lc&uuml;
    52  */
    53  public class LevelRangeFilter extends Filter {
    54
    55    /**
    56       Do we return ACCEPT when a match occurs. Default is
    57       <code>false</code>, so that later filters get run by default  */
    58    boolean acceptOnMatch = false;
    59
    60    Level levelMin;
    61    Level levelMax;
    62
    63
    64    /**
    65       Return the decision of this filter.
    66     */
    67    public
    68    int decide(LoggingEvent event) {
    69      if(this.levelMin != null) {
    70        if (event.getLevel().isGreaterOrEqual(levelMin) == false) {
    71          // level of event is less than minimum
    72          return Filter.DENY;
    73        }
    74      }
    75
    76      if(this.levelMax != null) {
    77        if (event.getLevel().toInt() > levelMax.toInt()) {
    78          // level of event is greater than maximum
    79          // Alas, there is no Level.isGreater method. and using
    80          // a combo of isGreaterOrEqual && !Equal seems worse than
    81          // checking the int values of the level objects..
    82          return Filter.DENY;
    83        }
    84      }
    85
    86      if (acceptOnMatch) {
    87        // this filter set up to bypass later filters and always return
    88        // accept if level in range
    89        return Filter.ACCEPT;
    90      }
    91      else {
    92        // event is ok for this filter; allow later filters to have a look..
    93        return Filter.NEUTRAL;
    94      }
    95    }
    96
    97   /**
    98       Get the value of the <code>LevelMax</code> option.  */
    99    public
    100   Level getLevelMax() {
    101     return levelMax;
    102   }
    103
    104
    105   /**
    106      Get the value of the <code>LevelMin</code> option.  */
    107   public
    108   Level getLevelMin() {
    109     return levelMin;
    110   }
    111
    112   /**
    113      Get the value of the <code>AcceptOnMatch</code> option.
    114    */
    115   public
    116   boolean getAcceptOnMatch() {
    117     return acceptOnMatch;
    118   }
    119
    120   /**
    121      Set the <code>LevelMax</code> option.
    122    */
    123   public
    124   void setLevelMax(Level levelMax) {
    125     this.levelMax =  levelMax;
    126   }
    127
    128   /**
    129      Set the <code>LevelMin</code> option.
    130    */
    131   public
    132   void setLevelMin(Level levelMin) {
    133     this.levelMin =  levelMin;
    134   }
    135
    136   /**
    137      Set the <code>AcceptOnMatch</code> option.
    138    */
    139   public
    140   void setAcceptOnMatch(boolean acceptOnMatch) {
    141     this.acceptOnMatch = acceptOnMatch;
    142   }
    143 }
    144