LoggerLevelRangeFilter.php 4.9 KB

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