1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-13 16:44:23 +02:00

Rename MinMax handler to Filter handler, wrap it up, refs #338

This commit is contained in:
Jordi Boggiano
2014-03-23 19:59:58 +01:00
parent c1d9256732
commit 605c4264cb
3 changed files with 99 additions and 37 deletions

View File

@@ -175,6 +175,8 @@ Handlers
for every log record. for every log record.
- _GroupHandler_: This handler groups other handlers. Every record received is - _GroupHandler_: This handler groups other handlers. Every record received is
sent to all the handlers it is configured with. sent to all the handlers it is configured with.
- _FilterHandler_: This handler only lets records of the given levels through
to the wrapped handler.
- _TestHandler_: Used for testing, it records everything that is sent to it and - _TestHandler_: Used for testing, it records everything that is sent to it and
has accessors to read out the information. has accessors to read out the information.

View File

@@ -5,11 +5,14 @@ namespace Monolog\Handler;
use Monolog\Logger; use Monolog\Logger;
/** /**
* Simple handler wrapper that processes only log entries, which are between the min and max log level. * Simple handler wrapper that filters records based on a list of levels
*
* It can be configured with an exact list of levels to allow, or a min/max level.
* *
* @author Hennadiy Verkh * @author Hennadiy Verkh
* @author Jordi Boggiano <j.boggiano@seld.be>
*/ */
class MinMaxHandler extends AbstractHandler class FilterHandler extends AbstractHandler
{ {
/** /**
* Handler or factory callable($record, $this) * Handler or factory callable($record, $this)
@@ -17,18 +20,14 @@ class MinMaxHandler extends AbstractHandler
* @var callable|\Monolog\Handler\HandlerInterface * @var callable|\Monolog\Handler\HandlerInterface
*/ */
protected $handler; protected $handler;
/** /**
* Minimum level for logs that are passes to handler * Minimum level for logs that are passes to handler
* *
* @var int * @var int
*/ */
protected $minLevel; protected $acceptedLevels;
/**
* Maximum level for logs that are passes to handler
*
* @var int
*/
protected $maxLevel;
/** /**
* Whether the messages that are handled can bubble up the stack or not * Whether the messages that are handled can bubble up the stack or not
* *
@@ -38,17 +37,39 @@ class MinMaxHandler extends AbstractHandler
/** /**
* @param callable|HandlerInterface $handler Handler or factory callable($record, $this). * @param callable|HandlerInterface $handler Handler or factory callable($record, $this).
* @param int $minLevel Minimum level for logs that are passes to handler * @param int|array $minLevelOrList A list of levels to accept or a minimum level if maxLevel is provided
* @param int $maxLevel Maximum level for logs that are passes to handler * @param int $maxLevel Maximum level to accept, only used if $minLevelOrList is an array
* @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
*/ */
public function __construct($handler, $minLevel = Logger::DEBUG, $maxLevel = Logger::EMERGENCY, $bubble = true) public function __construct($handler, $minLevelOrList = Logger::DEBUG, $maxLevel = Logger::EMERGENCY, $bubble = true)
{ {
$this->handler = $handler; $this->handler = $handler;
$this->minLevel = $minLevel;
$this->maxLevel = $maxLevel;
$this->bubble = $bubble; $this->bubble = $bubble;
$this->setAcceptedLevels($minLevelOrList, $maxLevel);
}
/**
* @return array
*/
public function getAcceptedLevels()
{
return array_flip($this->acceptedLevels);
}
/**
* @param int|array $minLevelOrList A list of levels to accept or a minimum level if maxLevel is provided
* @param int $maxLevel Maximum level to accept, only used if $minLevelOrList is an array
*/
public function setAcceptedLevels($minLevelOrList = Logger::DEBUG, $maxLevel = Logger::EMERGENCY)
{
if (is_array($minLevelOrList)) {
$acceptedLevels = $minLevelOrList;
} else {
$acceptedLevels = array_filter(Logger::getLevels(), function ($level) use ($minLevelOrList, $maxLevel) {
return $level >= $minLevelOrList && $level <= $maxLevel;
});
}
$this->acceptedLevels = array_flip($acceptedLevels);
} }
/** /**
@@ -56,7 +77,7 @@ class MinMaxHandler extends AbstractHandler
*/ */
public function isHandling(array $record) public function isHandling(array $record)
{ {
return $record['level'] >= $this->minLevel && $record['level'] <= $this->maxLevel; return isset($this->acceptedLevels[$record['level']]);
} }
/** /**
@@ -92,4 +113,19 @@ class MinMaxHandler extends AbstractHandler
return false === $this->bubble; return false === $this->bubble;
} }
/**
* {@inheritdoc}
*/
public function handleBatch(array $records)
{
$filtered = array();
foreach ($records as $record) {
if ($this->isHandling($record)) {
$filtered[] = $record;
}
}
$this->handler->handleBatch($filtered);
}
} }

View File

@@ -5,21 +5,15 @@ namespace Monolog\Handler;
use Monolog\Logger; use Monolog\Logger;
use Monolog\TestCase; use Monolog\TestCase;
/** class FilterHandlerTest extends TestCase
* Unit tests for minMaxHandler
*
* @author Hennadiy Verkh
*/
class MinMaxHandlerTest extends TestCase
{ {
/** /**
* @covers Monolog\Handler\MinMaxHandler::isHandling * @covers Monolog\Handler\FilterHandler::isHandling
*/ */
public function testIsHandling() public function testIsHandling()
{ {
$test = new TestHandler(); $test = new TestHandler();
$handler = new MinMaxHandler($test, Logger::INFO, Logger::NOTICE); $handler = new FilterHandler($test, Logger::INFO, Logger::NOTICE);
$this->assertFalse($handler->isHandling($this->getRecord(Logger::DEBUG))); $this->assertFalse($handler->isHandling($this->getRecord(Logger::DEBUG)));
$this->assertTrue($handler->isHandling($this->getRecord(Logger::INFO))); $this->assertTrue($handler->isHandling($this->getRecord(Logger::INFO)));
$this->assertTrue($handler->isHandling($this->getRecord(Logger::NOTICE))); $this->assertTrue($handler->isHandling($this->getRecord(Logger::NOTICE)));
@@ -31,12 +25,14 @@ class MinMaxHandlerTest extends TestCase
} }
/** /**
* @covers Monolog\Handler\MinMaxHandler::handle * @covers Monolog\Handler\FilterHandler::handle
* @covers Monolog\Handler\FilterHandler::setAcceptedLevels
* @covers Monolog\Handler\FilterHandler::isHandling
*/ */
public function testHandleProcessOnlyNeededLevels() public function testHandleProcessOnlyNeededLevels()
{ {
$test = new TestHandler(); $test = new TestHandler();
$handler = new MinMaxHandler($test, Logger::INFO, Logger::NOTICE); $handler = new FilterHandler($test, Logger::INFO, Logger::NOTICE);
$handler->handle($this->getRecord(Logger::DEBUG)); $handler->handle($this->getRecord(Logger::DEBUG));
$this->assertFalse($test->hasDebugRecords()); $this->assertFalse($test->hasDebugRecords());
@@ -56,15 +52,43 @@ class MinMaxHandlerTest extends TestCase
$this->assertFalse($test->hasAlertRecords()); $this->assertFalse($test->hasAlertRecords());
$handler->handle($this->getRecord(Logger::EMERGENCY)); $handler->handle($this->getRecord(Logger::EMERGENCY));
$this->assertFalse($test->hasEmergencyRecords()); $this->assertFalse($test->hasEmergencyRecords());
$test = new TestHandler();
$handler = new FilterHandler($test, array(Logger::INFO, Logger::ERROR));
$handler->handle($this->getRecord(Logger::DEBUG));
$this->assertFalse($test->hasDebugRecords());
$handler->handle($this->getRecord(Logger::INFO));
$this->assertTrue($test->hasInfoRecords());
$handler->handle($this->getRecord(Logger::NOTICE));
$this->assertFalse($test->hasNoticeRecords());
$handler->handle($this->getRecord(Logger::ERROR));
$this->assertTrue($test->hasErrorRecords());
$handler->handle($this->getRecord(Logger::CRITICAL));
$this->assertFalse($test->hasCriticalRecords());
} }
/** /**
* @covers Monolog\Handler\MinMaxHandler::handle * @covers Monolog\Handler\FilterHandler::setAcceptedLevels
* @covers Monolog\Handler\FilterHandler::getAcceptedLevels
*/
public function testAcceptedLevelApi()
{
$test = new TestHandler();
$handler = new FilterHandler($test);
$levels = array(Logger::INFO, Logger::ERROR);
$handler->setAcceptedLevels($levels);
$this->assertSame($levels, $handler->getAcceptedLevels());
}
/**
* @covers Monolog\Handler\FilterHandler::handle
*/ */
public function testHandleUsesProcessors() public function testHandleUsesProcessors()
{ {
$test = new TestHandler(); $test = new TestHandler();
$handler = new MinMaxHandler($test, Logger::DEBUG, Logger::EMERGENCY); $handler = new FilterHandler($test, Logger::DEBUG, Logger::EMERGENCY);
$handler->pushProcessor( $handler->pushProcessor(
function ($record) { function ($record) {
$record['extra']['foo'] = true; $record['extra']['foo'] = true;
@@ -79,28 +103,28 @@ class MinMaxHandlerTest extends TestCase
} }
/** /**
* @covers Monolog\Handler\MinMaxHandler::handle * @covers Monolog\Handler\FilterHandler::handle
*/ */
public function testHandleRespectsBubble() public function testHandleRespectsBubble()
{ {
$test = new TestHandler(); $test = new TestHandler();
$handler = new MinMaxHandler($test, Logger::INFO, Logger::NOTICE, false); $handler = new FilterHandler($test, Logger::INFO, Logger::NOTICE, false);
$this->assertTrue($handler->handle($this->getRecord(Logger::INFO))); $this->assertTrue($handler->handle($this->getRecord(Logger::INFO)));
$this->assertFalse($handler->handle($this->getRecord(Logger::WARNING))); $this->assertFalse($handler->handle($this->getRecord(Logger::WARNING)));
$handler = new MinMaxHandler($test, Logger::INFO, Logger::NOTICE, true); $handler = new FilterHandler($test, Logger::INFO, Logger::NOTICE, true);
$this->assertFalse($handler->handle($this->getRecord(Logger::INFO))); $this->assertFalse($handler->handle($this->getRecord(Logger::INFO)));
$this->assertFalse($handler->handle($this->getRecord(Logger::WARNING))); $this->assertFalse($handler->handle($this->getRecord(Logger::WARNING)));
} }
/** /**
* @covers Monolog\Handler\MinMaxHandler::handle * @covers Monolog\Handler\FilterHandler::handle
*/ */
public function testHandleWithCallback() public function testHandleWithCallback()
{ {
$test = new TestHandler(); $test = new TestHandler();
$handler = new MinMaxHandler( $handler = new FilterHandler(
function ($record, $handler) use ($test) { function ($record, $handler) use ($test) {
return $test; return $test;
}, Logger::INFO, Logger::NOTICE, false }, Logger::INFO, Logger::NOTICE, false
@@ -112,16 +136,16 @@ class MinMaxHandlerTest extends TestCase
} }
/** /**
* @covers Monolog\Handler\MinMaxHandler::handle * @covers Monolog\Handler\FilterHandler::handle
* @expectedException \RuntimeException * @expectedException \RuntimeException
*/ */
public function testHandleWithBadCallbackThrowsException() public function testHandleWithBadCallbackThrowsException()
{ {
$handler = new MinMaxHandler( $handler = new FilterHandler(
function ($record, $handler) { function ($record, $handler) {
return 'foo'; return 'foo';
} }
); );
$handler->handle($this->getRecord(Logger::WARNING)); $handler->handle($this->getRecord(Logger::WARNING));
} }
} }