library/log4php/filters/LoggerFilterStringMatch.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 string matching.
       
    23  *
       
    24  * <p>The filter admits two options {@link $stringToMatch} and
       
    25  * {@link $acceptOnMatch}. If there is a match (using {@link PHP_MANUAL#strpos}
       
    26  * between the value of the {@link $stringToMatch} option and the message
       
    27  * of the {@link LoggerLoggingEvent},
       
    28  * then the {@link decide()} method returns {@link LoggerFilter::ACCEPT} if
       
    29  * the <b>AcceptOnMatch</b> option value is true, if it is false then
       
    30  * {@link LoggerFilter::DENY} is returned. If there is no match, {@link LoggerFilter::NEUTRAL}
       
    31  * is returned.</p>
       
    32  *
       
    33  * <p>
       
    34  * An example for this filter:
       
    35  *
       
    36  * {@example ../../examples/php/filter_stringmatch.php 19}
       
    37  *
       
    38  * <p>
       
    39  * The corresponding XML file:
       
    40  *
       
    41  * {@example ../../examples/resources/filter_stringmatch.xml 18}
       
    42  *
       
    43  * @version $Revision: 1213283 $
       
    44  * @package log4php
       
    45  * @subpackage filters
       
    46  * @since 0.3
       
    47  */
       
    48 class LoggerFilterStringMatch extends LoggerFilter {
       
    49 
       
    50     /**
       
    51      * @var boolean
       
    52      */
       
    53     protected $acceptOnMatch = true;
       
    54 
       
    55     /**
       
    56      * @var string
       
    57      */
       
    58     protected $stringToMatch;
       
    59 
       
    60     /**
       
    61      * @param mixed $acceptOnMatch a boolean or a string ('true' or 'false')
       
    62      */
       
    63     public function setAcceptOnMatch($acceptOnMatch) {
       
    64         $this->setBoolean('acceptOnMatch', $acceptOnMatch);
       
    65     }
       
    66 
       
    67     /**
       
    68      * @param string $s the string to match
       
    69      */
       
    70     public function setStringToMatch($string) {
       
    71         $this->setString('stringToMatch', $string);
       
    72     }
       
    73 
       
    74     /**
       
    75      * @return integer a {@link LOGGER_FILTER_NEUTRAL} is there is no string match.
       
    76      */
       
    77     public function decide(LoggerLoggingEvent $event) {
       
    78         $msg = $event->getRenderedMessage();
       
    79 
       
    80         if ($msg === null or $this->stringToMatch === null) {
       
    81             return LoggerFilter::NEUTRAL;
       
    82         }
       
    83 
       
    84         if (strpos($msg, $this->stringToMatch) !== false) {
       
    85             return ($this->acceptOnMatch) ? LoggerFilter::ACCEPT : LoggerFilter::DENY;
       
    86         }
       
    87         return LoggerFilter::NEUTRAL;
       
    88     }
       
    89 }