source: branches/comu-ver2/data/module/log4php/php5/log4php/varia/LoggerStringMatchFilter.php @ 18220

Revision 18220, 3.1 KB checked in by yokkuns, 11 years ago (diff)

#149 ロガークラス作成

Line 
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 *
19 * @package log4php
20 * @subpackage varia
21 */
22
23/**
24 * @ignore
25 */
26if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
27 
28/**
29 */
30require_once(LOG4PHP_DIR . '/spi/LoggerFilter.php');
31
32/**
33 * This is a very simple filter based on string matching.
34 *
35 * <p>The filter admits two options {@link $stringToMatch} and
36 * {@link $acceptOnMatch}. If there is a match (using {@link PHP_MANUAL#strpos}
37 * between the value of the {@link $stringToMatch} option and the message
38 * of the {@link LoggerLoggingEvent},
39 * then the {@link decide()} method returns {@link LOG4PHP_LOGGER_FILTER_ACCEPT} if
40 * the <b>AcceptOnMatch</b> option value is true, if it is false then
41 * {@link LOG4PHP_LOGGER_FILTER_DENY} is returned. If there is no match, {@link LOG4PHP_LOGGER_FILTER_NEUTRAL}
42 * is returned.</p>
43 *
44 * @author  Marco Vassura
45 * @version $Revision: 635069 $
46 * @package log4php
47 * @subpackage varia
48 * @since 0.3
49 */
50class LoggerStringMatchFilter extends LoggerFilter {
51 
52    /**
53     * @var boolean
54     */
55    var $acceptOnMatch = true;
56
57    /**
58     * @var string
59     */
60    var $stringToMatch = null;
61 
62    /**
63     * @return boolean
64     */
65    function getAcceptOnMatch()
66    {
67        return $this->acceptOnMatch;
68    }
69   
70    /**
71     * @param mixed $acceptOnMatch a boolean or a string ('true' or 'false')
72     */
73    function setAcceptOnMatch($acceptOnMatch)
74    {
75        $this->acceptOnMatch = is_bool($acceptOnMatch) ?
76            $acceptOnMatch :
77            (bool)(strtolower($acceptOnMatch) == 'true');
78    }
79   
80    /**
81     * @return string
82     */
83    function getStringToMatch()
84    {
85        return $this->stringToMatch;
86    }
87   
88    /**
89     * @param string $s the string to match
90     */
91    function setStringToMatch($s)
92    {
93        $this->stringToMatch = $s;
94    }
95
96    /**
97     * @return integer a {@link LOGGER_FILTER_NEUTRAL} is there is no string match.
98     */
99    function decide($event)
100    {
101        $msg = $event->getRenderedMessage();
102       
103        if($msg === null or  $this->stringToMatch === null)
104            return LOG4PHP_LOGGER_FILTER_NEUTRAL;
105        if( strpos($msg, $this->stringToMatch) !== false ) {
106            return ($this->acceptOnMatch) ? LOG4PHP_LOGGER_FILTER_ACCEPT : LOG4PHP_LOGGER_FILTER_DENY ;
107        }
108        return LOG4PHP_LOGGER_FILTER_NEUTRAL;       
109    }
110}
Note: See TracBrowser for help on using the repository browser.