LoggerFilterLevelRange.php
4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @package log4php
*/
/**
* This is a very simple filter based on level matching, which can be
* used to reject messages with priorities outside a certain range.
*
* <p>The filter admits three options <b><var>LevelMin</var></b>, <b><var>LevelMax</var></b>
* and <b><var>AcceptOnMatch</var></b>.</p>
*
* <p>If the level of the {@link LoggerLoggingEvent} is not between Min and Max
* (inclusive), then {@link LoggerFilter::DENY} is returned.</p>
*
* <p>If the Logging event level is within the specified range, then if
* <b><var>AcceptOnMatch</var></b> is <i>true</i>,
* {@link LoggerFilter::ACCEPT} is returned, and if
* <b><var>AcceptOnMatch</var></b> is <i>false</i>,
* {@link LoggerFilter::NEUTRAL} is returned.</p>
*
* <p>If <b><var>LevelMin</var></b> is not defined, then there is no
* minimum acceptable level (i.e. a level is never rejected for
* being too "low"/unimportant). If <b><var>LevelMax</var></b> is not
* defined, then there is no maximum acceptable level (ie a
* level is never rejected for being too "high"/important).</p>
*
* <p>Refer to the {@link LoggerAppender::setThreshold()} method
* available to <b>all</b> appenders extending {@link LoggerAppender}
* for a more convenient way to filter out events by level.</p>
*
* <p>
* An example for this filter:
*
* {@example ../../examples/php/filter_levelrange.php 19}
*
* <p>
* The corresponding XML file:
*
* {@example ../../examples/resources/filter_levelrange.xml 18}
*
* @author Simon Kitching
* @author based on the org.apache.log4j.varia.LevelRangeFilte Java code by Ceki Gülcü
*
* @version $Revision: 883108 $
* @package log4php
* @subpackage filters
* @since 0.6
*/
class LoggerFilterLevelRange extends LoggerFilter {
/**
* @var boolean
*/
private $acceptOnMatch = true;
/**
* @var LoggerLevel
*/
private $levelMin;
/**
* @var LoggerLevel
*/
private $levelMax;
/**
* @param boolean $acceptOnMatch
*/
public function setAcceptOnMatch($acceptOnMatch) {
$this->acceptOnMatch = LoggerOptionConverter::toBoolean($acceptOnMatch, true);
}
/**
* @param string $l the level min to match
*/
public function setLevelMin($l) {
if($l instanceof LoggerLevel) {
$this->levelMin = $l;
} else {
$this->levelMin = LoggerOptionConverter::toLevel($l, null);
}
}
/**
* @param string $l the level max to match
*/
public function setLevelMax($l) {
if($l instanceof LoggerLevel) {
$this->levelMax = $l;
} else {
$this->levelMax = LoggerOptionConverter::toLevel($l, null);
}
}
/**
* Return the decision of this filter.
*
* @param LoggerLoggingEvent $event
* @return integer
*/
public function decide(LoggerLoggingEvent $event) {
$level = $event->getLevel();
if($this->levelMin !== null) {
if($level->isGreaterOrEqual($this->levelMin) == false) {
// level of event is less than minimum
return LoggerFilter::DENY;
}
}
if($this->levelMax !== null) {
if($level->toInt() > $this->levelMax->toInt()) {
// level of event is greater than maximum
// Alas, there is no Level.isGreater method. and using
// a combo of isGreaterOrEqual && !Equal seems worse than
// checking the int values of the level objects..
return LoggerFilter::DENY;
}
}
if($this->acceptOnMatch) {
// this filter set up to bypass later filters and always return
// accept if level in range
return LoggerFilter::ACCEPT;
} else {
// event is ok for this filter; allow later filters to have a look..
return LoggerFilter::NEUTRAL;
}
}
}