From c72aff22389c6b522761cc9f6f7252a35a4e435b Mon Sep 17 00:00:00 2001 From: Mike Meessen Date: Mon, 29 Apr 2013 14:52:29 +0200 Subject: [PATCH 1/3] Added CategoryErrorLevelActivationStrategy, which enables setting the Error Level threshold per log channel. --- .../CategoryErrorLevelActivationStrategy.php | 44 +++++++++++++++++++ .../Handler/FingersCrossedHandlerTest.php | 17 +++++++ 2 files changed, 61 insertions(+) create mode 100644 src/Monolog/Handler/FingersCrossed/CategoryErrorLevelActivationStrategy.php diff --git a/src/Monolog/Handler/FingersCrossed/CategoryErrorLevelActivationStrategy.php b/src/Monolog/Handler/FingersCrossed/CategoryErrorLevelActivationStrategy.php new file mode 100644 index 00000000..338c2a4e --- /dev/null +++ b/src/Monolog/Handler/FingersCrossed/CategoryErrorLevelActivationStrategy.php @@ -0,0 +1,44 @@ + +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code. +*/ + +namespace Monolog\Handler\FingersCrossed; + +/** + * Channel and Error level based monolog activation strategy. Allows to trigger activation + * based on level per channel. e.g. trigger activation on level 'ERROR' by default, except + * for records of the 'sql' channel; those should trigger activation on level 'WARN'. + * + * @author Mike Meessen + */ +class CategoryErrorLevelActivationStrategy implements ActivationStrategyInterface +{ + private $defaultActionLevel; + private $channelToActionLevel; + + /** + * @param int $defaultActionLevel The default action level to be used if the record's category doesn't match any + * @param array $categoryToActionLevel An array that maaps category names to action levels. + */ + public function __construct($defaultActionLevel, $channelToActionLevel = array()) + { + $this->defaultActionLevel = $defaultActionLevel; + $this->channelToActionLevel = $channelToActionLevel; + } + + public function isHandlerActivated(array $record) + { + if (isset($this->channelToActionLevel[$record['channel']])) { + return $record['level'] >= $this->channelToActionLevel[$record['channel']]; + } else { + return $record['level'] >= $this->defaultActionLevel; + } + } +} diff --git a/tests/Monolog/Handler/FingersCrossedHandlerTest.php b/tests/Monolog/Handler/FingersCrossedHandlerTest.php index c2207115..6c741618 100644 --- a/tests/Monolog/Handler/FingersCrossedHandlerTest.php +++ b/tests/Monolog/Handler/FingersCrossedHandlerTest.php @@ -14,6 +14,7 @@ namespace Monolog\Handler; use Monolog\TestCase; use Monolog\Logger; use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; +use Monolog\Handler\FingersCrossed\CategoryErrorLevelActivationStrategy; class FingersCrossedHandlerTest extends TestCase { @@ -149,6 +150,22 @@ class FingersCrossedHandlerTest extends TestCase $this->assertTrue($test->hasWarningRecords()); } + /** + * @covers Monolog\Handler\FingersCrossedHandler::__construct + */ + public function testCategoryErrorLevelActivationStrategy() + { + $test = new TestHandler(); + $handler = new FingersCrossedHandler($test, new CategoryErrorLevelActivationStrategy(Logger::ERROR, array('othertest' => Logger::DEBUG))); + $handler->handle($this->getRecord(Logger::WARNING)); + $this->assertFalse($test->hasWarningRecords()); + $record = $this->getRecord(Logger::DEBUG); + $record['channel'] = 'othertest'; + $handler->handle($record); + $this->assertTrue($test->hasDebugRecords()); + $this->assertTrue($test->hasWarningRecords()); + } + /** * @covers Monolog\Handler\FingersCrossedHandler::handle */ From 690bbdb2ffc467a462fc4174284001366a7fb5d8 Mon Sep 17 00:00:00 2001 From: Mike Meessen Date: Tue, 30 Apr 2013 10:57:25 +0200 Subject: [PATCH 2/3] Refactored / Renamed the CategoryErrorLevelActivationStrategy to the more appropriate ChannelLevelActivationStrategy. --- ...ivationStrategy.php => ChannelLevelActivationStrategy.php} | 2 +- tests/Monolog/Handler/FingersCrossedHandlerTest.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/Monolog/Handler/FingersCrossed/{CategoryErrorLevelActivationStrategy.php => ChannelLevelActivationStrategy.php} (94%) diff --git a/src/Monolog/Handler/FingersCrossed/CategoryErrorLevelActivationStrategy.php b/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php similarity index 94% rename from src/Monolog/Handler/FingersCrossed/CategoryErrorLevelActivationStrategy.php rename to src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php index 338c2a4e..7c38cebb 100644 --- a/src/Monolog/Handler/FingersCrossed/CategoryErrorLevelActivationStrategy.php +++ b/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php @@ -18,7 +18,7 @@ namespace Monolog\Handler\FingersCrossed; * * @author Mike Meessen */ -class CategoryErrorLevelActivationStrategy implements ActivationStrategyInterface +class ChannelLevelActivationStrategy implements ActivationStrategyInterface { private $defaultActionLevel; private $channelToActionLevel; diff --git a/tests/Monolog/Handler/FingersCrossedHandlerTest.php b/tests/Monolog/Handler/FingersCrossedHandlerTest.php index 6c741618..c38e0a59 100644 --- a/tests/Monolog/Handler/FingersCrossedHandlerTest.php +++ b/tests/Monolog/Handler/FingersCrossedHandlerTest.php @@ -14,7 +14,7 @@ namespace Monolog\Handler; use Monolog\TestCase; use Monolog\Logger; use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; -use Monolog\Handler\FingersCrossed\CategoryErrorLevelActivationStrategy; +use Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy; class FingersCrossedHandlerTest extends TestCase { @@ -156,7 +156,7 @@ class FingersCrossedHandlerTest extends TestCase public function testCategoryErrorLevelActivationStrategy() { $test = new TestHandler(); - $handler = new FingersCrossedHandler($test, new CategoryErrorLevelActivationStrategy(Logger::ERROR, array('othertest' => Logger::DEBUG))); + $handler = new FingersCrossedHandler($test, new ChannelLevelActivationStrategy(Logger::ERROR, array('othertest' => Logger::DEBUG))); $handler->handle($this->getRecord(Logger::WARNING)); $this->assertFalse($test->hasWarningRecords()); $record = $this->getRecord(Logger::DEBUG); From b2ce4483a5af444a2b154ea9d8a7c875a42535af Mon Sep 17 00:00:00 2001 From: Mike Meessen Date: Tue, 30 Apr 2013 12:57:02 +0200 Subject: [PATCH 3/3] Fixed phpdoc, typo and indentation issues. --- .../ChannelLevelActivationStrategy.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php b/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php index 7c38cebb..3b9d1616 100644 --- a/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php +++ b/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php @@ -25,7 +25,7 @@ class ChannelLevelActivationStrategy implements ActivationStrategyInterface /** * @param int $defaultActionLevel The default action level to be used if the record's category doesn't match any - * @param array $categoryToActionLevel An array that maaps category names to action levels. + * @param array $categoryToActionLevel An array that maps channel names to action levels. */ public function __construct($defaultActionLevel, $channelToActionLevel = array()) { @@ -35,10 +35,10 @@ class ChannelLevelActivationStrategy implements ActivationStrategyInterface public function isHandlerActivated(array $record) { - if (isset($this->channelToActionLevel[$record['channel']])) { - return $record['level'] >= $this->channelToActionLevel[$record['channel']]; - } else { - return $record['level'] >= $this->defaultActionLevel; - } + if (isset($this->channelToActionLevel[$record['channel']])) { + return $record['level'] >= $this->channelToActionLevel[$record['channel']]; + } else { + return $record['level'] >= $this->defaultActionLevel; + } } }