From d24900bade2b6a9e909087bd4f6192efa817250c Mon Sep 17 00:00:00 2001 From: Alex Pounds Date: Sat, 4 May 2013 22:02:10 +0100 Subject: [PATCH 1/5] Ignore vim swapfiles. --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index 8e63c675..82da0099 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,7 @@ composer.lock # PhpStorm project files .idea + +# Vim swapfiles. +.*.swp +.*.swo From 509e6cb0618dc2921fef1a311a7aade25a03a371 Mon Sep 17 00:00:00 2001 From: Alex Pounds Date: Sat, 4 May 2013 22:02:47 +0100 Subject: [PATCH 2/5] A new filter to match against logger names. This logging filter matches a logger name. Included is an example of its use, the filter itself, and a unit test for this functionality. --- src/examples/php/filter_namematch.php | 26 +++++ src/examples/resources/filter_namematch.xml | 30 +++++ src/main/php/LoggerAutoloader.php | 27 ++--- .../php/filters/LoggerFilterNameMatch.php | 109 ++++++++++++++++++ .../php/filters/LoggerFilterNameMatchTest.php | 90 +++++++++++++++ 5 files changed, 269 insertions(+), 13 deletions(-) create mode 100644 src/examples/php/filter_namematch.php create mode 100644 src/examples/resources/filter_namematch.xml create mode 100644 src/main/php/filters/LoggerFilterNameMatch.php create mode 100644 src/test/php/filters/LoggerFilterNameMatchTest.php diff --git a/src/examples/php/filter_namematch.php b/src/examples/php/filter_namematch.php new file mode 100644 index 00000000..8815748d --- /dev/null +++ b/src/examples/php/filter_namematch.php @@ -0,0 +1,26 @@ +info("Messages from foo are denied due to the second filter"); +$loggerBar->info("Messages from bar are accepted"); diff --git a/src/examples/resources/filter_namematch.xml b/src/examples/resources/filter_namematch.xml new file mode 100644 index 00000000..6059bef0 --- /dev/null +++ b/src/examples/resources/filter_namematch.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + diff --git a/src/main/php/LoggerAutoloader.php b/src/main/php/LoggerAutoloader.php index 86098c51..6346e235 100644 --- a/src/main/php/LoggerAutoloader.php +++ b/src/main/php/LoggerAutoloader.php @@ -6,15 +6,15 @@ * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - * + * * @package log4php */ @@ -26,15 +26,15 @@ /** * Class autoloader. - * + * * @package log4php * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0 */ class LoggerAutoloader { - + /** Maps classnames to files containing the class. */ private static $classes = array( - + // Base 'LoggerAppender' => '/LoggerAppender.php', 'LoggerAppenderPool' => '/LoggerAppenderPool.php', @@ -52,7 +52,7 @@ class LoggerAutoloader { 'LoggerReflectionUtils' => '/LoggerReflectionUtils.php', 'LoggerRoot' => '/LoggerRoot.php', 'LoggerThrowableInformation' => '/LoggerThrowableInformation.php', - + // Appenders 'LoggerAppenderConsole' => '/appenders/LoggerAppenderConsole.php', 'LoggerAppenderDailyFile' => '/appenders/LoggerAppenderDailyFile.php', @@ -68,7 +68,7 @@ class LoggerAutoloader { 'LoggerAppenderRollingFile' => '/appenders/LoggerAppenderRollingFile.php', 'LoggerAppenderSocket' => '/appenders/LoggerAppenderSocket.php', 'LoggerAppenderSyslog' => '/appenders/LoggerAppenderSyslog.php', - + // Configurators 'LoggerConfigurationAdapter' => '/configurators/LoggerConfigurationAdapter.php', 'LoggerConfigurationAdapterINI' => '/configurators/LoggerConfigurationAdapterINI.php', @@ -79,7 +79,8 @@ class LoggerAutoloader { // Filters 'LoggerFilterDenyAll' => '/filters/LoggerFilterDenyAll.php', 'LoggerFilterLevelMatch' => '/filters/LoggerFilterLevelMatch.php', - 'LoggerFilterLevelRange' => '/filters/LoggerFilterLevelRange.php', + 'LoggerFilterLevelRange' => '/filters/LoggerFilterLevelRange.php', + 'LoggerFilterNameMatch' => '/filters/LoggerFilterNameMatch.php', 'LoggerFilterStringMatch' => '/filters/LoggerFilterStringMatch.php', // Helpers @@ -87,7 +88,7 @@ class LoggerAutoloader { 'LoggerOptionConverter' => '/helpers/LoggerOptionConverter.php', 'LoggerPatternParser' => '/helpers/LoggerPatternParser.php', 'LoggerUtils' => '/helpers/LoggerUtils.php', - + // Pattern converters 'LoggerPatternConverter' => '/pattern/LoggerPatternConverter.php', 'LoggerPatternConverterClass' => '/pattern/LoggerPatternConverterClass.php', @@ -113,7 +114,7 @@ class LoggerAutoloader { 'LoggerPatternConverterSessionID' => '/pattern/LoggerPatternConverterSessionID.php', 'LoggerPatternConverterSuperglobal' => '/pattern/LoggerPatternConverterSuperglobal.php', 'LoggerPatternConverterThrowable' => '/pattern/LoggerPatternConverterThrowable.php', - + // Layouts 'LoggerLayoutHtml' => '/layouts/LoggerLayoutHtml.php', 'LoggerLayoutPattern' => '/layouts/LoggerLayoutPattern.php', @@ -121,14 +122,14 @@ class LoggerAutoloader { 'LoggerLayoutSimple' => '/layouts/LoggerLayoutSimple.php', 'LoggerLayoutTTCC' => '/layouts/LoggerLayoutTTCC.php', 'LoggerLayoutXml' => '/layouts/LoggerLayoutXml.php', - + // Renderers 'LoggerRendererDefault' => '/renderers/LoggerRendererDefault.php', 'LoggerRendererException' => '/renderers/LoggerRendererException.php', 'LoggerRendererMap' => '/renderers/LoggerRendererMap.php', 'LoggerRenderer' => '/renderers/LoggerRenderer.php', ); - + /** * Loads a class. * @param string $className The name of the class to load. diff --git a/src/main/php/filters/LoggerFilterNameMatch.php b/src/main/php/filters/LoggerFilterNameMatch.php new file mode 100644 index 00000000..6daa7afe --- /dev/null +++ b/src/main/php/filters/LoggerFilterNameMatch.php @@ -0,0 +1,109 @@ +The filter admits three options: {@link $stringToMatch}, {@link $caseSensitive}, and + * {@link $acceptOnMatch}. If there is a match (using {@link PHP_MANUAL#strpos} + * between the value of the {@link $stringToMatch} option and the name + * 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. Matching is case-sensitive by default. Setting {@link $caseSensitive} + * to false makes matching case insensitive.

+ * + *

+ * An example for this filter: + * + * {@example ../../examples/php/filter_namematch.php 19} + * + *

+ * The corresponding XML file: + * + * {@example ../../examples/resources/filter_namematch.xml 18} + * + * @package log4php + * @subpackage filters + * @since 2.3.1 + */ +class LoggerFilterNameMatch extends LoggerFilter { + + /** + * @var boolean + */ + protected $acceptOnMatch = true; + + /** + * @var boolean + */ + protected $caseSensitive = 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 mixed $caseSensitive a boolean or a string ('true' or 'false') + */ + public function setCaseSensitive($caseSensitive) { + $this->setBoolean('caseSensitive', $caseSensitive); + } + + /** + * @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->getLoggerName(); + + if($msg === null or $this->stringToMatch === null) { + return LoggerFilter::NEUTRAL; + } + + if($this->caseSensitive) { + return $this->testString((function_exists('mb_strpos') ? 'mb_strpos' : 'strpos'), $msg); + } else { + return $this->testString((function_exists('mb_stripos') ? 'mb_stripos' : 'stripos'), $msg); + } + } + + protected function testString($method, $msg) { + if($method($msg, $this->stringToMatch) !== false) { + return ($this->acceptOnMatch) ? LoggerFilter::ACCEPT : LoggerFilter::DENY; + } + return LoggerFilter::NEUTRAL; + } +} diff --git a/src/test/php/filters/LoggerFilterNameMatchTest.php b/src/test/php/filters/LoggerFilterNameMatchTest.php new file mode 100644 index 00000000..fa6750dd --- /dev/null +++ b/src/test/php/filters/LoggerFilterNameMatchTest.php @@ -0,0 +1,90 @@ +setAcceptOnMatch("true"); + $filter->setStringToMatch("AcCePtEd"); + + $eventFromAccepted = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('AcCePtEd'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromAccepted2 = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accepted'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromElsewhere = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Elsewhere'), LoggerLevel::getLevelInfo(), "Irrelevant"); + + + // Events are case-sensitive by default. + $this->assertEquals($filter->decide($eventFromAccepted), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromAccepted2), LoggerFilter::NEUTRAL); + $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); + + // But we can make them case insensitive. + $filter->setCaseSensitive("false"); + $this->assertEquals($filter->decide($eventFromAccepted), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromAccepted2), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); + } + + public function testPartialMatch() { + $filter = new LoggerFilterNameMatch(); + $filter->setAcceptOnMatch("true"); + $filter->setStringToMatch("Accept"); + + $eventFromAccept = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accept'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromAccepted = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accepted'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromElsewhere = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Elsewhere'), LoggerLevel::getLevelInfo(), "Irrelevant"); + + // Partial matches are accepted. + $this->assertEquals($filter->decide($eventFromAccept), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromAccepted), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); + } + + public function testAcceptOnMatchTrue() { + $filter = new LoggerFilterNameMatch(); + $filter->setAcceptOnMatch("true"); + $filter->setStringToMatch("Accept"); + + $eventFromAccept = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accept'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromNeutral = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Neutral'), LoggerLevel::getLevelInfo(), "Irrelevant"); + + $this->assertEquals($filter->decide($eventFromAccept), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromNeutral), LoggerFilter::NEUTRAL); + } + + public function testAcceptOnMatchFalse() { + $filter = new LoggerFilterNameMatch(); + $filter->setAcceptOnMatch("false"); + $filter->setStringToMatch("Deny"); + + $eventFromDeny = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Deny'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromNeutral = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Neutral'), LoggerLevel::getLevelInfo(), "Irrelevant"); + + $this->assertEquals($filter->decide($eventFromDeny), LoggerFilter::DENY); + $this->assertEquals($filter->decide($eventFromNeutral), LoggerFilter::NEUTRAL); + } +} From 204b816bd631169476576acb1f0aea5da276aca4 Mon Sep 17 00:00:00 2001 From: Alex Pounds Date: Sat, 4 May 2013 22:24:40 +0100 Subject: [PATCH 3/5] Support for matching logger names exactly. --- .../php/filters/LoggerFilterNameMatch.php | 39 ++++++++++++++----- .../php/filters/LoggerFilterNameMatchTest.php | 16 ++++++++ 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/main/php/filters/LoggerFilterNameMatch.php b/src/main/php/filters/LoggerFilterNameMatch.php index 6daa7afe..50b7daee 100644 --- a/src/main/php/filters/LoggerFilterNameMatch.php +++ b/src/main/php/filters/LoggerFilterNameMatch.php @@ -21,15 +21,15 @@ /** * This is a very simple filter based on string matching. * - *

The filter admits three options: {@link $stringToMatch}, {@link $caseSensitive}, and - * {@link $acceptOnMatch}. If there is a match (using {@link PHP_MANUAL#strpos} - * between the value of the {@link $stringToMatch} option and the name - * 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} + *

The filter admits four options: {@link $stringToMatch}, {@link $caseSensitive}, + * {@link $exactMatch}, and + * {@link $acceptOnMatch}. If the value of the {@link $stringToMatch} option is included + * in name of the {@link LoggerLoggingEvent}, then the {@link decide()} method returns + * {@link LoggerFilter::ACCEPT} if the AcceptOnMatch option value is true and + * {@link LoggerFilter::DENY} if it is false. If there is no match, {@link LoggerFilter::NEUTRAL} * is returned. Matching is case-sensitive by default. Setting {@link $caseSensitive} - * to false makes matching case insensitive.

+ * to false makes matching case insensitive. An exact match can be required by setting + * {@link $exactMatch} to true.

* *

* An example for this filter: @@ -57,6 +57,11 @@ class LoggerFilterNameMatch extends LoggerFilter { */ protected $caseSensitive = true; + /** + * @var boolean + */ + protected $exactMatch = false; + /** * @var string */ @@ -76,6 +81,13 @@ public function setCaseSensitive($caseSensitive) { $this->setBoolean('caseSensitive', $caseSensitive); } + /** + * @param mixed $caseSensitive a boolean or a string ('true' or 'false') + */ + public function setExactMatch($exactMatch) { + $this->setBoolean('exactMatch', $exactMatch); + } + /** * @param string $s the string to match */ @@ -100,9 +112,18 @@ public function decide(LoggerLoggingEvent $event) { } } + protected function testString($method, $msg) { if($method($msg, $this->stringToMatch) !== false) { - return ($this->acceptOnMatch) ? LoggerFilter::ACCEPT : LoggerFilter::DENY; + if($this->exactMatch) { + $lenFunc = function_exists('mb_strlen') ? 'mb_strlen' : 'strlen'; + if($lenFunc($this->stringToMatch) === $lenFunc($msg)) { + // We were looking for an exact match, and we found one. + return ($this->acceptOnMatch) ? LoggerFilter::ACCEPT : LoggerFilter::DENY; + } + } else { // No exact match required. + return ($this->acceptOnMatch) ? LoggerFilter::ACCEPT : LoggerFilter::DENY; + } } return LoggerFilter::NEUTRAL; } diff --git a/src/test/php/filters/LoggerFilterNameMatchTest.php b/src/test/php/filters/LoggerFilterNameMatchTest.php index fa6750dd..eba1d7d3 100644 --- a/src/test/php/filters/LoggerFilterNameMatchTest.php +++ b/src/test/php/filters/LoggerFilterNameMatchTest.php @@ -64,6 +64,22 @@ public function testPartialMatch() { $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); } + public function testExactMatch() { + $filter = new LoggerFilterNameMatch(); + $filter->setAcceptOnMatch("true"); + $filter->setStringToMatch("Accept"); + $filter->setExactMatch("true"); + + $eventFromAccept = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accept'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromAccepted = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accepted'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromElsewhere = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Elsewhere'), LoggerLevel::getLevelInfo(), "Irrelevant"); + + // Partial matches are accepted. + $this->assertEquals($filter->decide($eventFromAccept), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromAccepted), LoggerFilter::NEUTRAL); + $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); + } + public function testAcceptOnMatchTrue() { $filter = new LoggerFilterNameMatch(); $filter->setAcceptOnMatch("true"); From c49e51221f103733b5f1324f97f5cfa51fe0f7fc Mon Sep 17 00:00:00 2001 From: Alex Pounds Date: Sat, 4 May 2013 22:58:50 +0100 Subject: [PATCH 4/5] Documentation for new Name filter; add details to changelog. --- src/changes/changes.xml | 3 +- src/site/xdoc/docs/filters.xml | 77 ++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index cb8b3d93..e60683ab 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -20,7 +20,8 @@ Apache log4php changelog - + + Added new filter for matching a logger name. Made Logger::isInitialized() public. Improved LoggerAppenderMail to set the Content-type header as defined in layout. diff --git a/src/site/xdoc/docs/filters.xml b/src/site/xdoc/docs/filters.xml index 555205d7..53c78c81 100644 --- a/src/site/xdoc/docs/filters.xml +++ b/src/site/xdoc/docs/filters.xml @@ -156,6 +156,10 @@ array( LoggerFilterLevelRange Filters based on logging event level range. + + LoggerFilterNameMatch + Filters by searching for a string in the logger name. + LoggerFilterStringMatch Filters by searching for a string in the logging event message. @@ -265,6 +269,79 @@ array( +]]> + + + + +

This filter allows or denies logging events if the logger name contains a given string. If no match + is found the filter remains neutral.

+ + +

Configurable parameters

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ParameterTypeRequiredDefaultDescription
stringToMatchLoggerLevelYes-The string to match.
caseSensitivebooleanNotrueIf true, matches are case sensitive. If false, matches ignore case differences.
exactMatchLoggerLevelNofalseIf true, the stringToMatch must have the same text content as the logger name. This + parameter co-operates with the caseSensitive parameter; that is, if your + match is case insensitive, then "Example" is considered an exact match with + "example".
acceptOnMatchbooleanNotrueIf true and a match is found, the matching log event is accepted. If false and a + match is found, the matching log event is denied.
+ +

Example

+ +

One common pattern is to give each class in your application a logger via + $this->logger = Logger::getLogger(__CLASS__) in the constructor. You could use this + filter to log any events from your Payment class for easier debugging and greater + visibility.

+ +

The following filter configuration only accepts events which contain the string "Payment" in + the logger name.

+ +

+    
+    
+
+
 ]]>
From 52b7d6825ba839969a2323ada60c542fe0e99365 Mon Sep 17 00:00:00 2001 From: Alex Pounds Date: Sun, 5 May 2013 01:27:12 +0100 Subject: [PATCH 5/5] Fix spacing (tabs instead of spaces). --- src/changes/changes.xml | 4 +- src/examples/resources/filter_namematch.xml | 4 +- src/main/php/LoggerAutoloader.php | 4 +- src/site/xdoc/docs/filters.xml | 28 ++-- .../php/filters/LoggerFilterNameMatchTest.php | 158 +++++++++--------- 5 files changed, 99 insertions(+), 99 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index e60683ab..0f13e74d 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -20,8 +20,8 @@ Apache log4php changelog - - Added new filter for matching a logger name. + + Added new filter for matching a logger name. Made Logger::isInitialized() public. Improved LoggerAppenderMail to set the Content-type header as defined in layout. diff --git a/src/examples/resources/filter_namematch.xml b/src/examples/resources/filter_namematch.xml index 6059bef0..b219019a 100644 --- a/src/examples/resources/filter_namematch.xml +++ b/src/examples/resources/filter_namematch.xml @@ -20,8 +20,8 @@ - - + + diff --git a/src/main/php/LoggerAutoloader.php b/src/main/php/LoggerAutoloader.php index 6346e235..c47459a4 100644 --- a/src/main/php/LoggerAutoloader.php +++ b/src/main/php/LoggerAutoloader.php @@ -79,8 +79,8 @@ class LoggerAutoloader { // Filters 'LoggerFilterDenyAll' => '/filters/LoggerFilterDenyAll.php', 'LoggerFilterLevelMatch' => '/filters/LoggerFilterLevelMatch.php', - 'LoggerFilterLevelRange' => '/filters/LoggerFilterLevelRange.php', - 'LoggerFilterNameMatch' => '/filters/LoggerFilterNameMatch.php', + 'LoggerFilterLevelRange' => '/filters/LoggerFilterLevelRange.php', + 'LoggerFilterNameMatch' => '/filters/LoggerFilterNameMatch.php', 'LoggerFilterStringMatch' => '/filters/LoggerFilterStringMatch.php', // Helpers diff --git a/src/site/xdoc/docs/filters.xml b/src/site/xdoc/docs/filters.xml index 53c78c81..000e650d 100644 --- a/src/site/xdoc/docs/filters.xml +++ b/src/site/xdoc/docs/filters.xml @@ -273,9 +273,9 @@ array( - -

This filter allows or denies logging events if the logger name contains a given string. If no match - is found the filter remains neutral.

+ +

This filter allows or denies logging events if the logger name contains a given string. If no match + is found the filter remains neutral.

Configurable parameters

@@ -300,7 +300,7 @@ array( caseSensitive - boolean + boolean No true If true, matches are case sensitive. If false, matches ignore case differences. @@ -310,28 +310,28 @@ array( LoggerLevel No false - If true, the stringToMatch must have the same text content as the logger name. This - parameter co-operates with the caseSensitive parameter; that is, if your - match is case insensitive, then "Example" is considered an exact match with - "example". + If true, the stringToMatch must have the same text content as the logger name. This + parameter co-operates with the caseSensitive parameter; that is, if your + match is case insensitive, then "Example" is considered an exact match with + "example". acceptOnMatch boolean No true - If true and a match is found, the matching log event is accepted. If false and a - match is found, the matching log event is denied. + If true and a match is found, the matching log event is accepted. If false and a + match is found, the matching log event is denied.

Example

-

One common pattern is to give each class in your application a logger via - $this->logger = Logger::getLogger(__CLASS__) in the constructor. You could use this - filter to log any events from your Payment class for easier debugging and greater - visibility.

+

One common pattern is to give each class in your application a logger via + $this->logger = Logger::getLogger(__CLASS__) in the constructor. You could use this + filter to log any events from your Payment class for easier debugging and greater + visibility.

The following filter configuration only accepts events which contain the string "Payment" in the logger name.

diff --git a/src/test/php/filters/LoggerFilterNameMatchTest.php b/src/test/php/filters/LoggerFilterNameMatchTest.php index eba1d7d3..b3316213 100644 --- a/src/test/php/filters/LoggerFilterNameMatchTest.php +++ b/src/test/php/filters/LoggerFilterNameMatchTest.php @@ -1,13 +1,13 @@ setAcceptOnMatch("true"); - $filter->setStringToMatch("AcCePtEd"); - - $eventFromAccepted = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('AcCePtEd'), LoggerLevel::getLevelInfo(), "Irrelevant"); - $eventFromAccepted2 = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accepted'), LoggerLevel::getLevelInfo(), "Irrelevant"); - $eventFromElsewhere = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Elsewhere'), LoggerLevel::getLevelInfo(), "Irrelevant"); - - - // Events are case-sensitive by default. - $this->assertEquals($filter->decide($eventFromAccepted), LoggerFilter::ACCEPT); - $this->assertEquals($filter->decide($eventFromAccepted2), LoggerFilter::NEUTRAL); - $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); - - // But we can make them case insensitive. - $filter->setCaseSensitive("false"); - $this->assertEquals($filter->decide($eventFromAccepted), LoggerFilter::ACCEPT); - $this->assertEquals($filter->decide($eventFromAccepted2), LoggerFilter::ACCEPT); - $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); - } - - public function testPartialMatch() { - $filter = new LoggerFilterNameMatch(); - $filter->setAcceptOnMatch("true"); - $filter->setStringToMatch("Accept"); - - $eventFromAccept = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accept'), LoggerLevel::getLevelInfo(), "Irrelevant"); - $eventFromAccepted = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accepted'), LoggerLevel::getLevelInfo(), "Irrelevant"); - $eventFromElsewhere = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Elsewhere'), LoggerLevel::getLevelInfo(), "Irrelevant"); - - // Partial matches are accepted. - $this->assertEquals($filter->decide($eventFromAccept), LoggerFilter::ACCEPT); - $this->assertEquals($filter->decide($eventFromAccepted), LoggerFilter::ACCEPT); - $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); - } - - public function testExactMatch() { - $filter = new LoggerFilterNameMatch(); - $filter->setAcceptOnMatch("true"); - $filter->setStringToMatch("Accept"); - $filter->setExactMatch("true"); - - $eventFromAccept = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accept'), LoggerLevel::getLevelInfo(), "Irrelevant"); - $eventFromAccepted = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accepted'), LoggerLevel::getLevelInfo(), "Irrelevant"); - $eventFromElsewhere = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Elsewhere'), LoggerLevel::getLevelInfo(), "Irrelevant"); - - // Partial matches are accepted. - $this->assertEquals($filter->decide($eventFromAccept), LoggerFilter::ACCEPT); - $this->assertEquals($filter->decide($eventFromAccepted), LoggerFilter::NEUTRAL); - $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); - } - - public function testAcceptOnMatchTrue() { - $filter = new LoggerFilterNameMatch(); - $filter->setAcceptOnMatch("true"); - $filter->setStringToMatch("Accept"); - - $eventFromAccept = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accept'), LoggerLevel::getLevelInfo(), "Irrelevant"); - $eventFromNeutral = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Neutral'), LoggerLevel::getLevelInfo(), "Irrelevant"); - - $this->assertEquals($filter->decide($eventFromAccept), LoggerFilter::ACCEPT); - $this->assertEquals($filter->decide($eventFromNeutral), LoggerFilter::NEUTRAL); - } - - public function testAcceptOnMatchFalse() { - $filter = new LoggerFilterNameMatch(); - $filter->setAcceptOnMatch("false"); - $filter->setStringToMatch("Deny"); - - $eventFromDeny = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Deny'), LoggerLevel::getLevelInfo(), "Irrelevant"); - $eventFromNeutral = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Neutral'), LoggerLevel::getLevelInfo(), "Irrelevant"); - - $this->assertEquals($filter->decide($eventFromDeny), LoggerFilter::DENY); - $this->assertEquals($filter->decide($eventFromNeutral), LoggerFilter::NEUTRAL); - } + public function testCaseSensitivity() { + $filter = new LoggerFilterNameMatch(); + $filter->setAcceptOnMatch("true"); + $filter->setStringToMatch("AcCePtEd"); + + $eventFromAccepted = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('AcCePtEd'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromAccepted2 = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accepted'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromElsewhere = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Elsewhere'), LoggerLevel::getLevelInfo(), "Irrelevant"); + + + // Events are case-sensitive by default. + $this->assertEquals($filter->decide($eventFromAccepted), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromAccepted2), LoggerFilter::NEUTRAL); + $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); + + // But we can make them case insensitive. + $filter->setCaseSensitive("false"); + $this->assertEquals($filter->decide($eventFromAccepted), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromAccepted2), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); + } + + public function testPartialMatch() { + $filter = new LoggerFilterNameMatch(); + $filter->setAcceptOnMatch("true"); + $filter->setStringToMatch("Accept"); + + $eventFromAccept = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accept'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromAccepted = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accepted'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromElsewhere = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Elsewhere'), LoggerLevel::getLevelInfo(), "Irrelevant"); + + // Partial matches are accepted. + $this->assertEquals($filter->decide($eventFromAccept), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromAccepted), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); + } + + public function testExactMatch() { + $filter = new LoggerFilterNameMatch(); + $filter->setAcceptOnMatch("true"); + $filter->setStringToMatch("Accept"); + $filter->setExactMatch("true"); + + $eventFromAccept = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accept'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromAccepted = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accepted'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromElsewhere = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Elsewhere'), LoggerLevel::getLevelInfo(), "Irrelevant"); + + // Partial matches are accepted. + $this->assertEquals($filter->decide($eventFromAccept), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromAccepted), LoggerFilter::NEUTRAL); + $this->assertEquals($filter->decide($eventFromElsewhere), LoggerFilter::NEUTRAL); + } + + public function testAcceptOnMatchTrue() { + $filter = new LoggerFilterNameMatch(); + $filter->setAcceptOnMatch("true"); + $filter->setStringToMatch("Accept"); + + $eventFromAccept = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Accept'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromNeutral = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Neutral'), LoggerLevel::getLevelInfo(), "Irrelevant"); + + $this->assertEquals($filter->decide($eventFromAccept), LoggerFilter::ACCEPT); + $this->assertEquals($filter->decide($eventFromNeutral), LoggerFilter::NEUTRAL); + } + + public function testAcceptOnMatchFalse() { + $filter = new LoggerFilterNameMatch(); + $filter->setAcceptOnMatch("false"); + $filter->setStringToMatch("Deny"); + + $eventFromDeny = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Deny'), LoggerLevel::getLevelInfo(), "Irrelevant"); + $eventFromNeutral = new LoggerLoggingEvent("LoggerFilterNameMatchTest", new Logger('Neutral'), LoggerLevel::getLevelInfo(), "Irrelevant"); + + $this->assertEquals($filter->decide($eventFromDeny), LoggerFilter::DENY); + $this->assertEquals($filter->decide($eventFromNeutral), LoggerFilter::NEUTRAL); + } }