1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-23 01:26:11 +02: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;
}
}