1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-16 10:04:14 +02:00

extracted activation logic from FingersCrossedHandler

This commit is contained in:
Johannes M. Schmitt
2012-03-19 01:55:18 -06:00
parent 6c15897ac7
commit 8cdc9c6e10
4 changed files with 74 additions and 8 deletions

View File

@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* 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 <schmittjoh@gmail.com>
*/
interface ActivationStrategyInterface
{
/**
* Returns whether the given record activates the handler.
*
* @param array $record
* @return Boolean
*/
function isHandlerActivated(array $record);
}

View File

@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* 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 <schmittjoh@gmail.com>
*/
class ErrorLevelActivationStrategy implements ActivationStrategyInterface
{
private $actionLevel;
public function __construct($actionLevel)
{
$this->actionLevel = $actionLevel;
}
public function isHandlerActivated(array $record)
{
return $record['level'] >= $this->actionLevel;
}
}

View File

@@ -11,6 +11,10 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\Handler\FingersCrossed\ActivationStrategyInterface;
use Monolog\Logger; use Monolog\Logger;
/** /**
@@ -25,7 +29,7 @@ use Monolog\Logger;
class FingersCrossedHandler extends AbstractHandler class FingersCrossedHandler extends AbstractHandler
{ {
protected $handler; protected $handler;
protected $actionLevel; protected $activationStrategy;
protected $buffering = true; protected $buffering = true;
protected $bufferSize; protected $bufferSize;
protected $buffer = array(); protected $buffer = array();
@@ -33,15 +37,15 @@ class FingersCrossedHandler extends AbstractHandler
/** /**
* @param callback|HandlerInterface $handler Handler or factory callback($record, $fingersCrossedHandler). * @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 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 $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) * @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->handler = $handler;
$this->actionLevel = $actionLevel; $this->activationStrategy = $activationStrategy ?: new ErrorLevelActivationStrategy(Logger::WARNING);
$this->bufferSize = $bufferSize; $this->bufferSize = $bufferSize;
$this->bubble = $bubble; $this->bubble = $bubble;
$this->stopBuffering = $stopBuffering; $this->stopBuffering = $stopBuffering;
@@ -65,7 +69,7 @@ class FingersCrossedHandler extends AbstractHandler
if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) { if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) {
array_shift($this->buffer); array_shift($this->buffer);
} }
if ($record['level'] >= $this->actionLevel) { if ($this->activationStrategy->isHandlerActivated($record)) {
if ($this->stopBuffering) { if ($this->stopBuffering) {
$this->buffering = false; $this->buffering = false;
} }

View File

@@ -11,6 +11,8 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Handler\FingersCrossed\ErrorLevelActivationStrategy;
use Monolog\TestCase; use Monolog\TestCase;
use Monolog\Logger; use Monolog\Logger;
@@ -69,7 +71,7 @@ class FingersCrossedHandlerTest extends TestCase
public function testHandleRestartBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled() public function testHandleRestartBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled()
{ {
$test = new TestHandler(); $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::DEBUG));
$handler->handle($this->getRecord(Logger::WARNING)); $handler->handle($this->getRecord(Logger::WARNING));
$handler->handle($this->getRecord(Logger::INFO)); $handler->handle($this->getRecord(Logger::INFO));
@@ -84,7 +86,7 @@ class FingersCrossedHandlerTest extends TestCase
public function testHandleBufferLimit() public function testHandleBufferLimit()
{ {
$test = new TestHandler(); $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::DEBUG)); $handler->handle($this->getRecord(Logger::DEBUG));
$handler->handle($this->getRecord(Logger::INFO)); $handler->handle($this->getRecord(Logger::INFO));
@@ -130,7 +132,7 @@ class FingersCrossedHandlerTest extends TestCase
public function testIsHandlingAlways() public function testIsHandlingAlways()
{ {
$test = new TestHandler(); $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))); $this->assertTrue($handler->isHandling($this->getRecord(Logger::DEBUG)));
} }
} }