diff --git a/library/log4php/filters/LoggerFilterStringMatch.php b/library/log4php/filters/LoggerFilterStringMatch.php new file mode 100644 --- /dev/null +++ b/library/log4php/filters/LoggerFilterStringMatch.php @@ -0,0 +1,89 @@ +The filter admits two options {@link $stringToMatch} and + * {@link $acceptOnMatch}. If there is a match (using {@link PHP_MANUAL#strpos} + * between the value of the {@link $stringToMatch} option and the message + * of the {@link LoggerLoggingEvent}, + * then the {@link decide()} method returns {@link LoggerFilter::ACCEPT} if + * the AcceptOnMatch option value is true, if it is false then + * {@link LoggerFilter::DENY} is returned. If there is no match, {@link LoggerFilter::NEUTRAL} + * is returned.
+ * + *+ * An example for this filter: + * + * {@example ../../examples/php/filter_stringmatch.php 19} + * + *
+ * The corresponding XML file: + * + * {@example ../../examples/resources/filter_stringmatch.xml 18} + * + * @version $Revision: 1213283 $ + * @package log4php + * @subpackage filters + * @since 0.3 + */ +class LoggerFilterStringMatch extends LoggerFilter { + + /** + * @var boolean + */ + protected $acceptOnMatch = true; + + /** + * @var string + */ + protected $stringToMatch; + + /** + * @param mixed $acceptOnMatch a boolean or a string ('true' or 'false') + */ + public function setAcceptOnMatch($acceptOnMatch) { + $this->setBoolean('acceptOnMatch', $acceptOnMatch); + } + + /** + * @param string $s the string to match + */ + public function setStringToMatch($string) { + $this->setString('stringToMatch', $string); + } + + /** + * @return integer a {@link LOGGER_FILTER_NEUTRAL} is there is no string match. + */ + public function decide(LoggerLoggingEvent $event) { + $msg = $event->getRenderedMessage(); + + if ($msg === null or $this->stringToMatch === null) { + return LoggerFilter::NEUTRAL; + } + + if (strpos($msg, $this->stringToMatch) !== false) { + return ($this->acceptOnMatch) ? LoggerFilter::ACCEPT : LoggerFilter::DENY; + } + return LoggerFilter::NEUTRAL; + } +}