1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-02-24 06:52:34 +01:00

Added MinMaxHandler with unit tests.

This commit is contained in:
Hennadiy Verkh 2014-03-17 15:06:35 +01:00
parent db7e2aa311
commit 447b7a549a
2 changed files with 179 additions and 0 deletions

View File

@ -0,0 +1,83 @@
<?php
namespace Monolog\Handler;
use Monolog\Logger;
/**
* Simple handler wrapper that processes only log entries, which are between the min and max log level.
*
* @author Hennadiy Verkh
*/
class MinMaxHandler extends AbstractHandler
{
/**
* Handler or factory callable($record, $fingersCrossedHandler)
*
* @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;
/**
* Whether the messages that are handled can bubble up the stack or not
*
* @var Boolean
*/
protected $bubble;
/**
* @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler).
* @param int $minLevel
* @param int $maxLevel
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
*
* @internal param \TtLibrary\Log\Handler\Maximum $int log level
*/
public function __construct($handler, $minLevel = Logger::DEBUG, $maxLevel = Logger::EMERGENCY, $bubble = true)
{
$this->handler = $handler;
$this->minLevel = $minLevel;
$this->maxLevel = $maxLevel;
$this->bubble = $bubble;
}
/**
* {@inheritdoc}
*/
public function isHandling(array $record)
{
return $record['level'] >= $this->minLevel && $record['level'] <= $this->maxLevel;
}
/**
* {@inheritdoc}
*/
public function handle(array $record)
{
if (!$this->isHandling($record)) {
return false;
}
if ($this->processors) {
foreach ($this->processors as $processor) {
$record = call_user_func($processor, $record);
}
}
$this->handler->handle($record);
return false === $this->bubble;
}
}

View File

@ -0,0 +1,96 @@
<?php
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\TestCase;
/**
* Unit tests for minMaxHandler
*
* @author Hennadiy Verkh
*/
class MinMaxHandlerTest extends TestCase
{
/**
* @covers Monolog\Handler\MinMaxHandler::isHandling
*/
public function testIsHandling()
{
$test = new TestHandler();
$handler = new MinMaxHandler($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)));
$this->assertFalse($handler->isHandling($this->getRecord(Logger::WARNING)));
$this->assertFalse($handler->isHandling($this->getRecord(Logger::ERROR)));
$this->assertFalse($handler->isHandling($this->getRecord(Logger::CRITICAL)));
$this->assertFalse($handler->isHandling($this->getRecord(Logger::ALERT)));
$this->assertFalse($handler->isHandling($this->getRecord(Logger::EMERGENCY)));
}
/**
* @covers Monolog\Handler\MinMaxHandler::handle
*/
public function testHandleProcessOnlyNeededLevels()
{
$test = new TestHandler();
$handler = new MinMaxHandler($test, Logger::INFO, Logger::NOTICE);
$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->assertTrue($test->hasNoticeRecords());
$handler->handle($this->getRecord(Logger::WARNING));
$this->assertFalse($test->hasWarningRecords());
$handler->handle($this->getRecord(Logger::ERROR));
$this->assertFalse($test->hasErrorRecords());
$handler->handle($this->getRecord(Logger::CRITICAL));
$this->assertFalse($test->hasCriticalRecords());
$handler->handle($this->getRecord(Logger::ALERT));
$this->assertFalse($test->hasAlertRecords());
$handler->handle($this->getRecord(Logger::EMERGENCY));
$this->assertFalse($test->hasEmergencyRecords());
}
/**
* @covers Monolog\Handler\MinMaxHandler::handle
*/
public function testHandleUsesProcessors()
{
$test = new TestHandler();
$handler = new MinMaxHandler($test, Logger::DEBUG, Logger::EMERGENCY);
$handler->pushProcessor(
function ($record) {
$record['extra']['foo'] = true;
return $record;
}
);
$handler->handle($this->getRecord(Logger::WARNING));
$this->assertTrue($test->hasWarningRecords());
$records = $test->getRecords();
$this->assertTrue($records[0]['extra']['foo']);
}
/**
* @covers Monolog\Handler\MinMaxHandler::handle
*/
public function testHandleRespectsBubble()
{
$test = new TestHandler();
$handler = new MinMaxHandler($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);
$this->assertFalse($handler->handle($this->getRecord(Logger::INFO)));
$this->assertFalse($handler->handle($this->getRecord(Logger::WARNING)));
}
}