From 8cdc9c6e107fba803c26f16d4361690494ebd9c9 Mon Sep 17 00:00:00 2001 From: "Johannes M. Schmitt" Date: Mon, 19 Mar 2012 01:55:18 -0600 Subject: [PATCH 1/2] extracted activation logic from FingersCrossedHandler --- .../ActivationStrategyInterface.php | 28 ++++++++++++++++ .../ErrorLevelActivationStrategy.php | 32 +++++++++++++++++++ src/Monolog/Handler/FingersCrossedHandler.php | 14 +++++--- .../Handler/FingersCrossedHandlerTest.php | 8 +++-- 4 files changed, 74 insertions(+), 8 deletions(-) create mode 100755 src/Monolog/Handler/FingersCrossed/ActivationStrategyInterface.php create mode 100755 src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php diff --git a/src/Monolog/Handler/FingersCrossed/ActivationStrategyInterface.php b/src/Monolog/Handler/FingersCrossed/ActivationStrategyInterface.php new file mode 100755 index 00000000..54b9a8d4 --- /dev/null +++ b/src/Monolog/Handler/FingersCrossed/ActivationStrategyInterface.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler\FingersCrossed; + +/** + * Interface for activation strategies for the FingersCrossedHandler. + * + * @author Johannes M. Schmitt + */ +interface ActivationStrategyInterface +{ + /** + * Returns whether the given record activates the handler. + * + * @param array $record + * @return Boolean + */ + function isHandlerActivated(array $record); +} \ No newline at end of file diff --git a/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php b/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php new file mode 100755 index 00000000..2cfe6dd0 --- /dev/null +++ b/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php @@ -0,0 +1,32 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler\FingersCrossed; + +/** + * Error level based activation strategy. + * + * @author Johannes M. Schmitt + */ +class ErrorLevelActivationStrategy implements ActivationStrategyInterface +{ + private $actionLevel; + + public function __construct($actionLevel) + { + $this->actionLevel = $actionLevel; + } + + public function isHandlerActivated(array $record) + { + return $record['level'] >= $this->actionLevel; + } +} \ No newline at end of file diff --git a/src/Monolog/Handler/FingersCrossedHandler.php b/src/Monolog/Handler/FingersCrossedHandler.php index 3086c735..3d7bb412 100644 --- a/src/Monolog/Handler/FingersCrossedHandler.php +++ b/src/Monolog/Handler/FingersCrossedHandler.php @@ -11,6 +11,10 @@ namespace Monolog\Handler; +use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; + +use Monolog\Handler\FingersCrossed\ActivationStrategyInterface; + use Monolog\Logger; /** @@ -25,7 +29,7 @@ use Monolog\Logger; class FingersCrossedHandler extends AbstractHandler { protected $handler; - protected $actionLevel; + protected $activationStrategy; protected $buffering = true; protected $bufferSize; protected $buffer = array(); @@ -33,15 +37,15 @@ class FingersCrossedHandler extends AbstractHandler /** * @param callback|HandlerInterface $handler Handler or factory callback($record, $fingersCrossedHandler). - * @param int $actionLevel The minimum logging level at which this handler will be triggered + * @param ActivationStrategyInterface $activationStrategy Strategy which determines when this handler takes action * @param int $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not * @param Boolean $stopBuffering Whether the handler should stop buffering after being triggered (default true) */ - public function __construct($handler, $actionLevel = Logger::WARNING, $bufferSize = 0, $bubble = true, $stopBuffering = true) + public function __construct($handler, ActivationStrategyInterface $activationStrategy = null, $bufferSize = 0, $bubble = true, $stopBuffering = true) { $this->handler = $handler; - $this->actionLevel = $actionLevel; + $this->activationStrategy = $activationStrategy ?: new ErrorLevelActivationStrategy(Logger::WARNING); $this->bufferSize = $bufferSize; $this->bubble = $bubble; $this->stopBuffering = $stopBuffering; @@ -65,7 +69,7 @@ class FingersCrossedHandler extends AbstractHandler if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) { array_shift($this->buffer); } - if ($record['level'] >= $this->actionLevel) { + if ($this->activationStrategy->isHandlerActivated($record)) { if ($this->stopBuffering) { $this->buffering = false; } diff --git a/tests/Monolog/Handler/FingersCrossedHandlerTest.php b/tests/Monolog/Handler/FingersCrossedHandlerTest.php index a26ebb06..88a44d0f 100644 --- a/tests/Monolog/Handler/FingersCrossedHandlerTest.php +++ b/tests/Monolog/Handler/FingersCrossedHandlerTest.php @@ -11,6 +11,8 @@ namespace Monolog\Handler; +use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; + use Monolog\TestCase; use Monolog\Logger; @@ -69,7 +71,7 @@ class FingersCrossedHandlerTest extends TestCase public function testHandleRestartBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled() { $test = new TestHandler(); - $handler = new FingersCrossedHandler($test, Logger::WARNING, 0, false, false); + $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING), 0, false, false); $handler->handle($this->getRecord(Logger::DEBUG)); $handler->handle($this->getRecord(Logger::WARNING)); $handler->handle($this->getRecord(Logger::INFO)); @@ -84,7 +86,7 @@ class FingersCrossedHandlerTest extends TestCase public function testHandleBufferLimit() { $test = new TestHandler(); - $handler = new FingersCrossedHandler($test, Logger::WARNING, 2); + $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING), 2); $handler->handle($this->getRecord(Logger::DEBUG)); $handler->handle($this->getRecord(Logger::DEBUG)); $handler->handle($this->getRecord(Logger::INFO)); @@ -130,7 +132,7 @@ class FingersCrossedHandlerTest extends TestCase public function testIsHandlingAlways() { $test = new TestHandler(); - $handler = new FingersCrossedHandler($test, Logger::ERROR); + $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::ERROR)); $this->assertTrue($handler->isHandling($this->getRecord(Logger::DEBUG))); } } From 8ee130dd242d82ff848d72978d741b66cc907fb0 Mon Sep 17 00:00:00 2001 From: "Johannes M. Schmitt" Date: Wed, 21 Mar 2012 20:16:42 -0600 Subject: [PATCH 2/2] made change BC --- src/Monolog/Handler/FingersCrossedHandler.php | 15 ++++++++++----- .../Monolog/Handler/FingersCrossedHandlerTest.php | 8 +++----- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Monolog/Handler/FingersCrossedHandler.php b/src/Monolog/Handler/FingersCrossedHandler.php index 3d7bb412..3731fa8c 100644 --- a/src/Monolog/Handler/FingersCrossedHandler.php +++ b/src/Monolog/Handler/FingersCrossedHandler.php @@ -12,9 +12,7 @@ namespace Monolog\Handler; use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; - use Monolog\Handler\FingersCrossed\ActivationStrategyInterface; - use Monolog\Logger; /** @@ -37,15 +35,22 @@ class FingersCrossedHandler extends AbstractHandler /** * @param callback|HandlerInterface $handler Handler or factory callback($record, $fingersCrossedHandler). - * @param ActivationStrategyInterface $activationStrategy Strategy which determines when this handler takes action + * @param int|ActivationStrategyInterface $activationStrategy Strategy which determines when this handler takes action * @param int $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not * @param Boolean $stopBuffering Whether the handler should stop buffering after being triggered (default true) */ - public function __construct($handler, ActivationStrategyInterface $activationStrategy = null, $bufferSize = 0, $bubble = true, $stopBuffering = true) + public function __construct($handler, $activationStrategy = null, $bufferSize = 0, $bubble = true, $stopBuffering = true) { + if (null === $activationStrategy) { + $activationStrategy = new ErrorLevelActivationStrategy(Logger::WARNING); + } + if (!$activationStrategy instanceof ActivationStrategyInterface) { + $activationStrategy = new ErrorLevelActivationStrategy($activationStrategy); + } + $this->handler = $handler; - $this->activationStrategy = $activationStrategy ?: new ErrorLevelActivationStrategy(Logger::WARNING); + $this->activationStrategy = $activationStrategy; $this->bufferSize = $bufferSize; $this->bubble = $bubble; $this->stopBuffering = $stopBuffering; diff --git a/tests/Monolog/Handler/FingersCrossedHandlerTest.php b/tests/Monolog/Handler/FingersCrossedHandlerTest.php index 88a44d0f..a26ebb06 100644 --- a/tests/Monolog/Handler/FingersCrossedHandlerTest.php +++ b/tests/Monolog/Handler/FingersCrossedHandlerTest.php @@ -11,8 +11,6 @@ namespace Monolog\Handler; -use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; - use Monolog\TestCase; use Monolog\Logger; @@ -71,7 +69,7 @@ class FingersCrossedHandlerTest extends TestCase public function testHandleRestartBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled() { $test = new TestHandler(); - $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING), 0, false, false); + $handler = new FingersCrossedHandler($test, Logger::WARNING, 0, false, false); $handler->handle($this->getRecord(Logger::DEBUG)); $handler->handle($this->getRecord(Logger::WARNING)); $handler->handle($this->getRecord(Logger::INFO)); @@ -86,7 +84,7 @@ class FingersCrossedHandlerTest extends TestCase public function testHandleBufferLimit() { $test = new TestHandler(); - $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING), 2); + $handler = new FingersCrossedHandler($test, Logger::WARNING, 2); $handler->handle($this->getRecord(Logger::DEBUG)); $handler->handle($this->getRecord(Logger::DEBUG)); $handler->handle($this->getRecord(Logger::INFO)); @@ -132,7 +130,7 @@ class FingersCrossedHandlerTest extends TestCase public function testIsHandlingAlways() { $test = new TestHandler(); - $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::ERROR)); + $handler = new FingersCrossedHandler($test, Logger::ERROR); $this->assertTrue($handler->isHandling($this->getRecord(Logger::DEBUG))); } }