[18220] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * log4php is a PHP port of the log4j java logging package.
|
---|
| 4 | *
|
---|
| 5 | * <p>This framework is based on log4j (see {@link http://jakarta.apache.org/log4j log4j} for details).</p>
|
---|
| 6 | * <p>Design, strategies and part of the methods documentation are developed by log4j team
|
---|
| 7 | * (Ceki Gülcü as log4j project founder and
|
---|
| 8 | * {@link http://jakarta.apache.org/log4j/docs/contributors.html contributors}).</p>
|
---|
| 9 | *
|
---|
| 10 | * <p>PHP port, extensions and modifications by VxR. All rights reserved.<br>
|
---|
| 11 | * For more information, please see {@link http://www.vxr.it/log4php/}.</p>
|
---|
| 12 | *
|
---|
| 13 | * <p>This software is published under the terms of the LGPL License
|
---|
| 14 | * a copy of which has been included with this distribution in the LICENSE file.</p>
|
---|
| 15 | *
|
---|
| 16 | * @package log4php
|
---|
| 17 | * @subpackage varia
|
---|
| 18 | */
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * @ignore
|
---|
| 22 | */
|
---|
| 23 | if (!defined('LOG4PHP_DIR')) define('LOG4PHP_DIR', dirname(__FILE__) . '/..');
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | */
|
---|
| 27 | require_once(LOG4PHP_DIR . '/spi/LoggerFilter.php');
|
---|
| 28 | require_once(LOG4PHP_DIR . '/LoggerLevel.php');
|
---|
| 29 | require_once(LOG4PHP_DIR . '/helpers/LoggerOptionConverter.php');
|
---|
| 30 |
|
---|
| 31 | /**
|
---|
| 32 | * This is a very simple filter based on level matching, which can be
|
---|
| 33 | * used to reject messages with priorities outside a certain range.
|
---|
| 34 | *
|
---|
| 35 | * <p>The filter admits three options <b><var>LevelMin</var></b>, <b><var>LevelMax</var></b>
|
---|
| 36 | * and <b><var>AcceptOnMatch</var></b>.</p>
|
---|
| 37 | *
|
---|
| 38 | * <p>If the level of the {@link LoggerLoggingEvent} is not between Min and Max
|
---|
| 39 | * (inclusive), then {@link LOG4PHP_LOGGER_FILTER_DENY} is returned.</p>
|
---|
| 40 | *
|
---|
| 41 | * <p>If the Logging event level is within the specified range, then if
|
---|
| 42 | * <b><var>AcceptOnMatch</var></b> is <i>true</i>,
|
---|
| 43 | * {@link LOG4PHP_LOGGER_FILTER_ACCEPT} is returned, and if
|
---|
| 44 | * <b><var>AcceptOnMatch</var></b> is <i>false</i>,
|
---|
| 45 | * {@link LOG4PHP_LOGGER_FILTER_NEUTRAL} is returned.</p>
|
---|
| 46 | *
|
---|
| 47 | * <p>If <b><var>LevelMin</var></b> is not defined, then there is no
|
---|
| 48 | * minimum acceptable level (ie a level is never rejected for
|
---|
| 49 | * being too "low"/unimportant). If <b><var>LevelMax</var></b> is not
|
---|
| 50 | * defined, then there is no maximum acceptable level (ie a
|
---|
| 51 | * level is never rejected for beeing too "high"/important).</p>
|
---|
| 52 | *
|
---|
| 53 | * <p>Refer to the {@link LoggerAppenderSkeleton::setThreshold()} method
|
---|
| 54 | * available to <b>all</b> appenders extending {@link LoggerAppenderSkeleton}
|
---|
| 55 | * for a more convenient way to filter out events by level.</p>
|
---|
| 56 | *
|
---|
| 57 | * @log4j-class org.apache.log4j.varia.LevelRangeFilter
|
---|
| 58 | * @log4j-author Simon Kitching
|
---|
| 59 | * @log4j-author based on code by Ceki Gülcü
|
---|
| 60 | *
|
---|
| 61 | * @author VxR <vxr@vxr.it>
|
---|
| 62 | * @version $Revision: 1.1 $
|
---|
| 63 | * @package log4php
|
---|
| 64 | * @subpackage varia
|
---|
| 65 | * @since 0.6
|
---|
| 66 | */
|
---|
| 67 | class LoggerLevelRangeFilter extends LoggerFilter {
|
---|
| 68 |
|
---|
| 69 | /**
|
---|
| 70 | * @var boolean
|
---|
| 71 | */
|
---|
| 72 | var $acceptOnMatch = true;
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * @var LoggerLevel
|
---|
| 76 | */
|
---|
| 77 | var $levelMin;
|
---|
| 78 |
|
---|
| 79 | /**
|
---|
| 80 | * @var LoggerLevel
|
---|
| 81 | */
|
---|
| 82 | var $levelMax;
|
---|
| 83 |
|
---|
| 84 | /**
|
---|
| 85 | * @return boolean
|
---|
| 86 | */
|
---|
| 87 | function getAcceptOnMatch()
|
---|
| 88 | {
|
---|
| 89 | return $this->acceptOnMatch;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /**
|
---|
| 93 | * @param boolean $acceptOnMatch
|
---|
| 94 | */
|
---|
| 95 | function setAcceptOnMatch($acceptOnMatch)
|
---|
| 96 | {
|
---|
| 97 | $this->acceptOnMatch = LoggerOptionConverter::toBoolean($acceptOnMatch, true);
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | /**
|
---|
| 101 | * @return LoggerLevel
|
---|
| 102 | */
|
---|
| 103 | function getLevelMin()
|
---|
| 104 | {
|
---|
| 105 | return $this->levelMin;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | /**
|
---|
| 109 | * @param string $l the level min to match
|
---|
| 110 | */
|
---|
| 111 | function setLevelMin($l)
|
---|
| 112 | {
|
---|
| 113 | $this->levelMin = LoggerOptionConverter::toLevel($l, null);
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /**
|
---|
| 117 | * @return LoggerLevel
|
---|
| 118 | */
|
---|
| 119 | function getLevelMax()
|
---|
| 120 | {
|
---|
| 121 | return $this->levelMax;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | /**
|
---|
| 125 | * @param string $l the level max to match
|
---|
| 126 | */
|
---|
| 127 | function setLevelMax($l)
|
---|
| 128 | {
|
---|
| 129 | $this->levelMax = LoggerOptionConverter::toLevel($l, null);
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | /**
|
---|
| 133 | * Return the decision of this filter.
|
---|
| 134 | *
|
---|
| 135 | * @param LoggerLoggingEvent $event
|
---|
| 136 | * @return integer
|
---|
| 137 | */
|
---|
| 138 | function decide($event)
|
---|
| 139 | {
|
---|
| 140 | $level = $event->getLevel();
|
---|
| 141 |
|
---|
| 142 | if($this->levelMin !== null) {
|
---|
| 143 | if ($level->isGreaterOrEqual($this->levelMin) == false) {
|
---|
| 144 | // level of event is less than minimum
|
---|
| 145 | return LOG4PHP_LOGGER_FILTER_DENY;
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | if($this->levelMax !== null) {
|
---|
| 150 | if ($level->toInt() > $this->levelMax->toInt()) {
|
---|
| 151 | // level of event is greater than maximum
|
---|
| 152 | // Alas, there is no Level.isGreater method. and using
|
---|
| 153 | // a combo of isGreaterOrEqual && !Equal seems worse than
|
---|
| 154 | // checking the int values of the level objects..
|
---|
| 155 | return LOG4PHP_LOGGER_FILTER_DENY;
|
---|
| 156 | }
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | if ($this->getAcceptOnMatch()) {
|
---|
| 160 | // this filter set up to bypass later filters and always return
|
---|
| 161 | // accept if level in range
|
---|
| 162 | return LOG4PHP_LOGGER_FILTER_ACCEPT;
|
---|
| 163 | } else {
|
---|
| 164 | // event is ok for this filter; allow later filters to have a look..
|
---|
| 165 | return LOG4PHP_LOGGER_FILTER_NEUTRAL;
|
---|
| 166 | }
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 | ?> |
---|