LoggerLevelMatchFilter.php 3.5 KB

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