1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 05:07:36 +02:00

Allowing some messages to always be flushed from FingersCrossedHandler

This commit is contained in:
David Simon
2014-05-27 11:26:42 -04:00
parent 93f8f3271f
commit b1216b8d45
2 changed files with 39 additions and 1 deletions

View File

@@ -35,6 +35,7 @@ class FingersCrossedHandler extends AbstractHandler
protected $bufferSize;
protected $buffer = array();
protected $stopBuffering;
protected $passthruLevel;
/**
* @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler).
@@ -42,8 +43,9 @@ class FingersCrossedHandler extends AbstractHandler
* @param int $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
* @param Boolean $stopBuffering Whether the handler should stop buffering after being triggered (default true)
* @param int $passthruLevel Minimum level to always flush to handler on close, even if strategy not triggered
*/
public function __construct($handler, $activationStrategy = null, $bufferSize = 0, $bubble = true, $stopBuffering = true)
public function __construct($handler, $activationStrategy = null, $bufferSize = 0, $bubble = true, $stopBuffering = true, $passthruLevel = NULL)
{
if (null === $activationStrategy) {
$activationStrategy = new ErrorLevelActivationStrategy(Logger::WARNING);
@@ -59,6 +61,7 @@ class FingersCrossedHandler extends AbstractHandler
$this->bufferSize = $bufferSize;
$this->bubble = $bubble;
$this->stopBuffering = $stopBuffering;
$this->passthruLevel = $passthruLevel;
}
/**
@@ -108,6 +111,23 @@ class FingersCrossedHandler extends AbstractHandler
return false === $this->bubble;
}
/**
* {@inheritdoc}
*/
public function close()
{
if (NULL !== $this->passthruLevel) {
$level = $this->passthruLevel;
$this->buffer = array_filter($this->buffer, function ($record) use ($level) {
return $record['level'] >= $level;
});
if (count($this->buffer) > 0) {
$this->handler->handleBatch($this->buffer);
$this->buffer = array();
}
}
}
/**
* Resets the state of the handler. Stops forwarding records to the wrapped handler.
*/

View File

@@ -31,6 +31,7 @@ class FingersCrossedHandlerTest extends TestCase
$this->assertFalse($test->hasDebugRecords());
$this->assertFalse($test->hasInfoRecords());
$handler->handle($this->getRecord(Logger::WARNING));
$handler->close();
$this->assertTrue($test->hasInfoRecords());
$this->assertTrue(count($test->getRecords()) === 3);
}
@@ -44,6 +45,7 @@ class FingersCrossedHandlerTest extends TestCase
$handler = new FingersCrossedHandler($test);
$handler->handle($this->getRecord(Logger::WARNING));
$handler->handle($this->getRecord(Logger::DEBUG));
$handler->close();
$this->assertTrue($test->hasWarningRecords());
$this->assertTrue($test->hasDebugRecords());
}
@@ -60,6 +62,7 @@ class FingersCrossedHandlerTest extends TestCase
$handler->handle($this->getRecord(Logger::DEBUG));
$handler->reset();
$handler->handle($this->getRecord(Logger::INFO));
$handler->close();
$this->assertTrue($test->hasWarningRecords());
$this->assertTrue($test->hasDebugRecords());
$this->assertFalse($test->hasInfoRecords());
@@ -75,6 +78,7 @@ class FingersCrossedHandlerTest extends TestCase
$handler->handle($this->getRecord(Logger::DEBUG));
$handler->handle($this->getRecord(Logger::WARNING));
$handler->handle($this->getRecord(Logger::INFO));
$handler->close();
$this->assertTrue($test->hasWarningRecords());
$this->assertTrue($test->hasDebugRecords());
$this->assertFalse($test->hasInfoRecords());
@@ -186,4 +190,18 @@ class FingersCrossedHandlerTest extends TestCase
$records = $test->getRecords();
$this->assertTrue($records[0]['extra']['foo']);
}
/**
* @covers Monolog\Handler\FingersCrossedHandler::close
*/
public function testPassthruOnClose()
{
$test = new TestHandler();
$handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING), 0, true, true, Logger::INFO);
$handler->handle($this->getRecord(Logger::DEBUG));
$handler->handle($this->getRecord(Logger::INFO));
$handler->close();
$this->assertFalse($test->hasDebugRecords());
$this->assertTrue($test->hasInfoRecords());
}
}