library/log4php/filters/LoggerFilterLevelMatch.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 /**
       
     3  * Licensed to the Apache Software Foundation (ASF) under one or more
       
     4  * contributor license agreements. See the NOTICE file distributed with
       
     5  * this work for additional information regarding copyright ownership.
       
     6  * The ASF licenses this file to You under the Apache License, Version 2.0
       
     7  * (the "License"); you may not use this file except in compliance with
       
     8  * the License. You may obtain a copy of the License at
       
     9  *
       
    10  *       http://www.apache.org/licenses/LICENSE-2.0
       
    11  *
       
    12  * Unless required by applicable law or agreed to in writing, software
       
    13  * distributed under the License is distributed on an "AS IS" BASIS,
       
    14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    15  * See the License for the specific language governing permissions and
       
    16  * limitations under the License.
       
    17  *
       
    18  * @package log4php
       
    19  */
       
    20 
       
    21 /**
       
    22  * This is a very simple filter based on level matching.
       
    23  *
       
    24  * <p>The filter admits two options <b><var>LevelToMatch</var></b> and
       
    25  * <b><var>AcceptOnMatch</var></b>. If there is an exact match between the value
       
    26  * of the <b><var>LevelToMatch</var></b> option and the level of the
       
    27  * {@link LoggerLoggingEvent}, then the {@link decide()} method returns
       
    28  * {@link LoggerFilter::ACCEPT} in case the <b><var>AcceptOnMatch</var></b>
       
    29  * option value is set to <i>true</i>, if it is <i>false</i> then
       
    30  * {@link LoggerFilter::DENY} is returned. If there is no match,
       
    31  * {@link LoggerFilter::NEUTRAL} is returned.</p>
       
    32  *
       
    33  * <p>
       
    34  * An example for this filter:
       
    35  *
       
    36  * {@example ../../examples/php/filter_levelmatch.php 19}
       
    37  *
       
    38  * <p>
       
    39  * The corresponding XML file:
       
    40  *
       
    41  * {@example ../../examples/resources/filter_levelmatch.xml 18}
       
    42  *
       
    43  * @version $Revision: 1213283 $
       
    44  * @package log4php
       
    45  * @subpackage filters
       
    46  * @since 0.6
       
    47  */
       
    48 class LoggerFilterLevelMatch extends LoggerFilter {
       
    49 
       
    50     /**
       
    51      * Indicates if this event should be accepted or denied on match
       
    52      * @var boolean
       
    53      */
       
    54     protected $acceptOnMatch = true;
       
    55 
       
    56     /**
       
    57      * The level, when to match
       
    58      * @var LoggerLevel
       
    59      */
       
    60     protected $levelToMatch;
       
    61 
       
    62     /**
       
    63      * @param boolean $acceptOnMatch
       
    64      */
       
    65     public function setAcceptOnMatch($acceptOnMatch) {
       
    66         $this->setBoolean('acceptOnMatch', $acceptOnMatch);
       
    67     }
       
    68 
       
    69     /**
       
    70      * @param string $l the level to match
       
    71      */
       
    72     public function setLevelToMatch($level) {
       
    73         $this->setLevel('levelToMatch', $level);
       
    74     }
       
    75 
       
    76     /**
       
    77      * Return the decision of this filter.
       
    78      *
       
    79      * Returns {@link LoggerFilter::NEUTRAL} if the <b><var>LevelToMatch</var></b>
       
    80      * option is not set or if there is not match.    Otherwise, if there is a
       
    81      * match, then the returned decision is {@link LoggerFilter::ACCEPT} if the
       
    82      * <b><var>AcceptOnMatch</var></b> property is set to <i>true</i>. The
       
    83      * returned decision is {@link LoggerFilter::DENY} if the
       
    84      * <b><var>AcceptOnMatch</var></b> property is set to <i>false</i>.
       
    85      *
       
    86      * @param LoggerLoggingEvent $event
       
    87      * @return integer
       
    88      */
       
    89     public function decide(LoggerLoggingEvent $event) {
       
    90         if ($this->levelToMatch === null) {
       
    91             return LoggerFilter::NEUTRAL;
       
    92         }
       
    93 
       
    94         if ($this->levelToMatch->equals($event->getLevel())) {
       
    95             return $this->acceptOnMatch ? LoggerFilter::ACCEPT : LoggerFilter::DENY;
       
    96         } else {
       
    97             return LoggerFilter::NEUTRAL;
       
    98         }
       
    99     }
       
   100 }