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:
@@ -175,6 +175,8 @@ Handlers
|
||||
for every log record.
|
||||
- _GroupHandler_: This handler groups other handlers. Every record received is
|
||||
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
|
||||
has accessors to read out the information.
|
||||
|
||||
|
@@ -5,11 +5,14 @@ namespace Monolog\Handler;
|
||||
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 Jordi Boggiano <j.boggiano@seld.be>
|
||||
*/
|
||||
class MinMaxHandler extends AbstractHandler
|
||||
class FilterHandler extends AbstractHandler
|
||||
{
|
||||
/**
|
||||
* Handler or factory callable($record, $this)
|
||||
@@ -17,18 +20,14 @@ class MinMaxHandler extends AbstractHandler
|
||||
* @var callable|\Monolog\Handler\HandlerInterface
|
||||
*/
|
||||
protected $handler;
|
||||
|
||||
/**
|
||||
* Minimum level for logs that are passes to handler
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $minLevel;
|
||||
/**
|
||||
* Maximum level for logs that are passes to handler
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
protected $maxLevel;
|
||||
protected $acceptedLevels;
|
||||
|
||||
/**
|
||||
* 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 int $minLevel Minimum level for logs that are passes to handler
|
||||
* @param int $maxLevel Maximum 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 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
|
||||
*/
|
||||
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->minLevel = $minLevel;
|
||||
$this->maxLevel = $maxLevel;
|
||||
$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)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function handleBatch(array $records)
|
||||
{
|
||||
$filtered = array();
|
||||
foreach ($records as $record) {
|
||||
if ($this->isHandling($record)) {
|
||||
$filtered[] = $record;
|
||||
}
|
||||
}
|
||||
|
||||
$this->handler->handleBatch($filtered);
|
||||
}
|
||||
}
|
@@ -5,21 +5,15 @@ namespace Monolog\Handler;
|
||||
use Monolog\Logger;
|
||||
use Monolog\TestCase;
|
||||
|
||||
/**
|
||||
* Unit tests for minMaxHandler
|
||||
*
|
||||
* @author Hennadiy Verkh
|
||||
*/
|
||||
class MinMaxHandlerTest extends TestCase
|
||||
class FilterHandlerTest extends TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @covers Monolog\Handler\MinMaxHandler::isHandling
|
||||
* @covers Monolog\Handler\FilterHandler::isHandling
|
||||
*/
|
||||
public function testIsHandling()
|
||||
{
|
||||
$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->assertTrue($handler->isHandling($this->getRecord(Logger::INFO)));
|
||||
$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()
|
||||
{
|
||||
$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));
|
||||
$this->assertFalse($test->hasDebugRecords());
|
||||
@@ -56,15 +52,43 @@ class MinMaxHandlerTest extends TestCase
|
||||
$this->assertFalse($test->hasAlertRecords());
|
||||
$handler->handle($this->getRecord(Logger::EMERGENCY));
|
||||
$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()
|
||||
{
|
||||
$test = new TestHandler();
|
||||
$handler = new MinMaxHandler($test, Logger::DEBUG, Logger::EMERGENCY);
|
||||
$handler = new FilterHandler($test, Logger::DEBUG, Logger::EMERGENCY);
|
||||
$handler->pushProcessor(
|
||||
function ($record) {
|
||||
$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()
|
||||
{
|
||||
$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->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::WARNING)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Monolog\Handler\MinMaxHandler::handle
|
||||
* @covers Monolog\Handler\FilterHandler::handle
|
||||
*/
|
||||
public function testHandleWithCallback()
|
||||
{
|
||||
$test = new TestHandler();
|
||||
$handler = new MinMaxHandler(
|
||||
$handler = new FilterHandler(
|
||||
function ($record, $handler) use ($test) {
|
||||
return $test;
|
||||
}, Logger::INFO, Logger::NOTICE, false
|
||||
@@ -112,12 +136,12 @@ class MinMaxHandlerTest extends TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Monolog\Handler\MinMaxHandler::handle
|
||||
* @covers Monolog\Handler\FilterHandler::handle
|
||||
* @expectedException \RuntimeException
|
||||
*/
|
||||
public function testHandleWithBadCallbackThrowsException()
|
||||
{
|
||||
$handler = new MinMaxHandler(
|
||||
$handler = new FilterHandler(
|
||||
function ($record, $handler) {
|
||||
return 'foo';
|
||||
}
|
Reference in New Issue
Block a user