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..3731fa8c 100644 --- a/src/Monolog/Handler/FingersCrossedHandler.php +++ b/src/Monolog/Handler/FingersCrossedHandler.php @@ -11,6 +11,8 @@ namespace Monolog\Handler; +use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy; +use Monolog\Handler\FingersCrossed\ActivationStrategyInterface; use Monolog\Logger; /** @@ -25,7 +27,7 @@ use Monolog\Logger; class FingersCrossedHandler extends AbstractHandler { protected $handler; - protected $actionLevel; + protected $activationStrategy; protected $buffering = true; protected $bufferSize; protected $buffer = array(); @@ -33,15 +35,22 @@ 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 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, $actionLevel = Logger::WARNING, $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->actionLevel = $actionLevel; + $this->activationStrategy = $activationStrategy; $this->bufferSize = $bufferSize; $this->bubble = $bubble; $this->stopBuffering = $stopBuffering; @@ -65,7 +74,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; }