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 | */ |
---|
26 | if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..'); |
---|
27 | |
---|
28 | /** |
---|
29 | */ |
---|
30 | require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php'); |
---|
31 | require_once(LOG4PHP_DIR . '/spi/LoggerFilter.php'); |
---|
32 | |
---|
33 | /** |
---|
34 | * This is a very simple filter based on level matching. |
---|
35 | * |
---|
36 | * <p>The filter admits two options <b><var>LevelToMatch</var></b> and |
---|
37 | * <b><var>AcceptOnMatch</var></b>. If there is an exact match between the value |
---|
38 | * of the <b><var>LevelToMatch</var></b> option and the level of the |
---|
39 | * {@link LoggerLoggingEvent}, then the {@link decide()} method returns |
---|
40 | * {@link LOG4PHP_LOGGER_FILTER_ACCEPT} in case the <b><var>AcceptOnMatch</var></b> |
---|
41 | * option value is set to <i>true</i>, if it is <i>false</i> then |
---|
42 | * {@link LOG4PHP_LOGGER_FILTER_DENY} is returned. If there is no match, |
---|
43 | * {@link LOG4PHP_LOGGER_FILTER_NEUTRAL} is returned.</p> |
---|
44 | * |
---|
45 | * @author Marco Vassura |
---|
46 | * @version $Revision: 635069 $ |
---|
47 | * @package log4php |
---|
48 | * @subpackage varia |
---|
49 | * @since 0.6 |
---|
50 | */ |
---|
51 | class LoggerLevelMatchFilter extends LoggerFilter { |
---|
52 | |
---|
53 | /** |
---|
54 | * @var boolean |
---|
55 | */ |
---|
56 | var $acceptOnMatch = true; |
---|
57 | |
---|
58 | /** |
---|
59 | * @var LoggerLevel |
---|
60 | */ |
---|
61 | var $levelToMatch; |
---|
62 | |
---|
63 | /** |
---|
64 | * @return boolean |
---|
65 | */ |
---|
66 | function getAcceptOnMatch() |
---|
67 | { |
---|
68 | return $this->acceptOnMatch; |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * @param boolean $acceptOnMatch |
---|
73 | */ |
---|
74 | function setAcceptOnMatch($acceptOnMatch) |
---|
75 | { |
---|
76 | $this->acceptOnMatch = LoggerOptionConverter::toBoolean($acceptOnMatch, true); |
---|
77 | } |
---|
78 | |
---|
79 | /** |
---|
80 | * @return LoggerLevel |
---|
81 | */ |
---|
82 | function getLevelToMatch() |
---|
83 | { |
---|
84 | return $this->levelToMatch; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * @param string $l the level to match |
---|
89 | */ |
---|
90 | function setLevelToMatch($l) |
---|
91 | { |
---|
92 | $this->levelToMatch = LoggerOptionConverter::toLevel($l, null); |
---|
93 | } |
---|
94 | |
---|
95 | /** |
---|
96 | * Return the decision of this filter. |
---|
97 | * |
---|
98 | * Returns {@link LOG4PHP_LOGGER_FILTER_NEUTRAL} if the <b><var>LevelToMatch</var></b> |
---|
99 | * option is not set or if there is not match. Otherwise, if there is a |
---|
100 | * match, then the returned decision is {@link LOG4PHP_LOGGER_FILTER_ACCEPT} if the |
---|
101 | * <b><var>AcceptOnMatch</var></b> property is set to <i>true</i>. The |
---|
102 | * returned decision is {@link LOG4PHP_LOGGER_FILTER_DENY} if the |
---|
103 | * <b><var>AcceptOnMatch</var></b> property is set to <i>false</i>. |
---|
104 | * |
---|
105 | * @param LoggerLoggingEvent $event |
---|
106 | * @return integer |
---|
107 | */ |
---|
108 | function decide($event) |
---|
109 | { |
---|
110 | if($this->levelToMatch === null) |
---|
111 | return LOG4PHP_LOGGER_FILTER_NEUTRAL; |
---|
112 | |
---|
113 | if ($this->levelToMatch->equals($event->getLevel())) { |
---|
114 | return $this->getAcceptOnMatch() ? |
---|
115 | LOG4PHP_LOGGER_FILTER_ACCEPT : |
---|
116 | LOG4PHP_LOGGER_FILTER_DENY; |
---|
117 | } else { |
---|
118 | return LOG4PHP_LOGGER_FILTER_NEUTRAL; |
---|
119 | } |
---|
120 | } |
---|
121 | } |
---|