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, which can be
23 : * used to reject messages with priorities outside a certain range.
24 : *
25 : * <p>The filter admits three options <b><var>LevelMin</var></b>, <b><var>LevelMax</var></b>
26 : * and <b><var>AcceptOnMatch</var></b>.</p>
27 : *
28 : * <p>If the level of the {@link LoggerLoggingEvent} is not between Min and Max
29 : * (inclusive), then {@link LoggerFilter::DENY} is returned.</p>
30 : *
31 : * <p>If the Logging event level is within the specified range, then if
32 : * <b><var>AcceptOnMatch</var></b> is <i>true</i>,
33 : * {@link LoggerFilter::ACCEPT} is returned, and if
34 : * <b><var>AcceptOnMatch</var></b> is <i>false</i>,
35 : * {@link LoggerFilter::NEUTRAL} is returned.</p>
36 : *
37 : * <p>If <b><var>LevelMin</var></b> is not defined, then there is no
38 : * minimum acceptable level (i.e. a level is never rejected for
39 : * being too "low"/unimportant). If <b><var>LevelMax</var></b> is not
40 : * defined, then there is no maximum acceptable level (ie a
41 : * level is never rejected for being too "high"/important).</p>
42 : *
43 : * <p>Refer to the {@link LoggerAppender::setThreshold()} method
44 : * available to <b>all</b> appenders extending {@link LoggerAppender}
45 : * for a more convenient way to filter out events by level.</p>
46 : *
47 : * <p>
48 : * An example for this filter:
49 : *
50 : * {@example ../../examples/php/filter_levelrange.php 19}
51 : *
52 : * <p>
53 : * The corresponding XML file:
54 : *
55 : * {@example ../../examples/resources/filter_levelrange.xml 18}
56 : *
57 : * @author Simon Kitching
58 : * @author based on the org.apache.log4j.varia.LevelRangeFilte Java code by Ceki Gülcü
59 : *
60 : * @version $Revision: 1213283 $
61 : * @package log4php
62 : * @subpackage filters
63 : * @since 0.6
64 : */
65 : class LoggerFilterLevelRange extends LoggerFilter {
66 :
67 : /**
68 : * @var boolean
69 : */
70 : protected $acceptOnMatch = true;
71 :
72 : /**
73 : * @var LoggerLevel
74 : */
75 : protected $levelMin;
76 :
77 : /**
78 : * @var LoggerLevel
79 : */
80 : protected $levelMax;
81 :
82 : /**
83 : * @param boolean $acceptOnMatch
84 : */
85 : public function setAcceptOnMatch($acceptOnMatch) {
86 2 : $this->setBoolean('acceptOnMatch', $acceptOnMatch);
87 2 : }
88 :
89 : /**
90 : * @param string $l the level min to match
91 : */
92 : public function setLevelMin($level) {
93 2 : $this->setLevel('levelMin', $level);
94 2 : }
95 :
96 : /**
97 : * @param string $l the level max to match
98 : */
99 : public function setLevelMax($level) {
100 2 : $this->setLevel('levelMax', $level);
101 2 : }
102 :
103 : /**
104 : * Return the decision of this filter.
105 : *
106 : * @param LoggerLoggingEvent $event
107 : * @return integer
108 : */
109 : public function decide(LoggerLoggingEvent $event) {
110 2 : $level = $event->getLevel();
111 :
112 2 : if($this->levelMin !== null) {
113 2 : if($level->isGreaterOrEqual($this->levelMin) == false) {
114 : // level of event is less than minimum
115 2 : return LoggerFilter::DENY;
116 : }
117 2 : }
118 :
119 2 : if($this->levelMax !== null) {
120 2 : if($level->toInt() > $this->levelMax->toInt()) {
121 : // level of event is greater than maximum
122 : // Alas, there is no Level.isGreater method. and using
123 : // a combo of isGreaterOrEqual && !Equal seems worse than
124 : // checking the int values of the level objects..
125 0 : return LoggerFilter::DENY;
126 : }
127 2 : }
128 :
129 2 : if($this->acceptOnMatch) {
130 : // this filter set up to bypass later filters and always return
131 : // accept if level in range
132 1 : return LoggerFilter::ACCEPT;
133 : } else {
134 : // event is ok for this filter; allow later filters to have a look..
135 1 : return LoggerFilter::NEUTRAL;
136 : }
137 : }
138 : }
|