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

Revision 18220, 5.0 KB checked in by yokkuns, 15 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 . '/helpers/LoggerOptionConverter.php');
31require_once(LOG4PHP_DIR . '/spi/LoggerFilter.php');
32
33/**
34 * This is a very simple filter based on level matching, which can be
35 * used to reject messages with priorities outside a certain range.
36 * 
37 * <p>The filter admits three options <b><var>LevelMin</var></b>, <b><var>LevelMax</var></b>
38 * and <b><var>AcceptOnMatch</var></b>.</p>
39 *
40 * <p>If the level of the {@link LoggerLoggingEvent} is not between Min and Max
41 * (inclusive), then {@link LOG4PHP_LOGGER_FILTER_DENY} is returned.</p>
42 * 
43 * <p>If the Logging event level is within the specified range, then if
44 * <b><var>AcceptOnMatch</var></b> is <i>true</i>,
45 * {@link LOG4PHP_LOGGER_FILTER_ACCEPT} is returned, and if
46 * <b><var>AcceptOnMatch</var></b> is <i>false</i>,
47 * {@link LOG4PHP_LOGGER_FILTER_NEUTRAL} is returned.</p>
48 * 
49 * <p>If <b><var>LevelMin</var></b> is not defined, then there is no
50 * minimum acceptable level (i.e. a level is never rejected for
51 * being too "low"/unimportant).  If <b><var>LevelMax</var></b> is not
52 * defined, then there is no maximum acceptable level (ie a
53 * level is never rejected for being too "high"/important).</p>
54 *
55 * <p>Refer to the {@link LoggerAppenderSkeleton::setThreshold()} method
56 * available to <b>all</b> appenders extending {@link LoggerAppenderSkeleton}
57 * for a more convenient way to filter out events by level.</p>
58 *
59 * @log4j-class org.apache.log4j.varia.LevelRangeFilter
60 * @log4j-author Simon Kitching
61 * @log4j-author based on code by Ceki G&uuml;lc&uuml;
62 *
63 * @author  Marco Vassura
64 * @version $Revision: 635069 $
65 * @package log4php
66 * @subpackage varia
67 * @since 0.6
68 */
69class LoggerLevelRangeFilter extends LoggerFilter {
70 
71    /**
72     * @var boolean
73     */
74    var $acceptOnMatch = true;
75
76    /**
77     * @var LoggerLevel
78     */
79    var $levelMin;
80 
81    /**
82     * @var LoggerLevel
83     */
84    var $levelMax;
85
86    /**
87     * @return boolean
88     */
89    function getAcceptOnMatch()
90    {
91        return $this->acceptOnMatch;
92    }
93   
94    /**
95     * @param boolean $acceptOnMatch
96     */
97    function setAcceptOnMatch($acceptOnMatch)
98    {
99        $this->acceptOnMatch = LoggerOptionConverter::toBoolean($acceptOnMatch, true);
100    }
101   
102    /**
103     * @return LoggerLevel
104     */
105    function getLevelMin()
106    {
107        return $this->levelMin;
108    }
109   
110    /**
111     * @param string $l the level min to match
112     */
113    function setLevelMin($l)
114    {
115        $this->levelMin = LoggerOptionConverter::toLevel($l, null);
116    }
117
118    /**
119     * @return LoggerLevel
120     */
121    function getLevelMax()
122    {
123        return $this->levelMax;
124    }
125   
126    /**
127     * @param string $l the level max to match
128     */
129    function setLevelMax($l)
130    {
131        $this->levelMax = LoggerOptionConverter::toLevel($l, null);
132    }
133
134    /**
135     * Return the decision of this filter.
136     *
137     * @param LoggerLoggingEvent $event
138     * @return integer
139     */
140    function decide($event)
141    {
142        $level = $event->getLevel();
143       
144        if($this->levelMin !== null) {
145            if ($level->isGreaterOrEqual($this->levelMin) == false) {
146                // level of event is less than minimum
147                return LOG4PHP_LOGGER_FILTER_DENY;
148            }
149        }
150
151        if($this->levelMax !== null) {
152            if ($level->toInt() > $this->levelMax->toInt()) {
153                // level of event is greater than maximum
154                // Alas, there is no Level.isGreater method. and using
155                // a combo of isGreaterOrEqual && !Equal seems worse than
156                // checking the int values of the level objects..
157                return LOG4PHP_LOGGER_FILTER_DENY;
158            }
159        }
160
161        if ($this->getAcceptOnMatch()) {
162            // this filter set up to bypass later filters and always return
163            // accept if level in range
164            return  LOG4PHP_LOGGER_FILTER_ACCEPT;
165        } else {
166            // event is ok for this filter; allow later filters to have a look..
167            return LOG4PHP_LOGGER_FILTER_NEUTRAL;
168        }
169    }
170}
Note: See TracBrowser for help on using the repository browser.