1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-03 11:47:38 +02:00

Added a level for the BufferHandler

This commit is contained in:
Christophe Coevoet
2011-04-07 12:49:09 +02:00
parent 0a09d83fb6
commit 2d959bf8b3
2 changed files with 21 additions and 1 deletions

View File

@@ -30,10 +30,12 @@ class BufferHandler extends AbstractHandler
/** /**
* @param HandlerInterface $handler Handler. * @param HandlerInterface $handler Handler.
* @param integer $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer. * @param integer $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
* @param integer $level The minimum logging level at which this handler will be triggered
* @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(HandlerInterface $handler, $bufferSize = 0, $bubble = false) public function __construct(HandlerInterface $handler, $bufferSize = 0, $level = Logger::DEBUG, $bubble = false)
{ {
parent::__construct($level, $bubble);
$this->handler = $handler; $this->handler = $handler;
$this->bufferSize = $bufferSize; $this->bufferSize = $bufferSize;
$this->bubble = $bubble; $this->bubble = $bubble;
@@ -44,6 +46,10 @@ class BufferHandler extends AbstractHandler
*/ */
public function handle(array $record) public function handle(array $record)
{ {
if ($record['level'] < $this->level) {
return false;
}
$this->buffer[] = $record; $this->buffer[] = $record;
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);

View File

@@ -53,4 +53,18 @@ class BufferHandlerTest extends TestCase
$this->assertTrue($test->hasInfoRecords()); $this->assertTrue($test->hasInfoRecords());
$this->assertFalse($test->hasDebugRecords()); $this->assertFalse($test->hasDebugRecords());
} }
public function testHandleLevel()
{
$test = new TestHandler();
$handler = new BufferHandler($test, 0, Logger::INFO);
$handler->handle($this->getRecord(Logger::DEBUG));
$handler->handle($this->getRecord(Logger::INFO));
$handler->handle($this->getRecord(Logger::WARNING));
$handler->handle($this->getRecord(Logger::DEBUG));
$handler->close();
$this->assertTrue($test->hasWarningRecords());
$this->assertTrue($test->hasInfoRecords());
$this->assertFalse($test->hasDebugRecords());
}
} }