1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-01 10:50:21 +02:00

Add regression test for #691 and optimize implementation using a single pass loop over the handlers, refs #692

This commit is contained in:
Jordi Boggiano
2015-11-26 23:48:42 +00:00
parent d4ae6ccffd
commit cab3ff71ff
2 changed files with 48 additions and 10 deletions

View File

@@ -280,11 +280,14 @@ class Logger implements LoggerInterface
// check if any handler will handle this message so we can return early and save cycles // check if any handler will handle this message so we can return early and save cycles
$handlerKey = null; $handlerKey = null;
foreach ($this->handlers as $key => $handler) { reset($this->handlers);
while ($handler = current($this->handlers)) {
if ($handler->isHandling(array('level' => $level))) { if ($handler->isHandling(array('level' => $level))) {
$handlerKey = $key; $handlerKey = key($this->handlers);
break; break;
} }
next($this->handlers);
} }
if (null === $handlerKey) { if (null === $handlerKey) {
@@ -315,17 +318,13 @@ class Logger implements LoggerInterface
foreach ($this->processors as $processor) { foreach ($this->processors as $processor) {
$record = call_user_func($processor, $record); $record = call_user_func($processor, $record);
} }
$foundStartingKey = false;
foreach ($this->handlers as $key => $handler) { while ($handler = current($this->handlers)) {
if ($key === $handlerKey) {
$foundStartingKey = true;
}
if ($foundStartingKey === false) {
continue;
}
if (true === $handler->handle($record)) { if (true === $handler->handle($record)) {
break; break;
} }
next($this->handlers);
} }
return true; return true;

View File

@@ -303,6 +303,45 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$logger->debug('test'); $logger->debug('test');
} }
/**
* @covers Monolog\Logger::addRecord
*/
public function testHandlersNotCalledBeforeFirstHandlingWithAssocArray()
{
$handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
$handler1->expects($this->never())
->method('isHandling')
->will($this->returnValue(false))
;
$handler1->expects($this->once())
->method('handle')
->will($this->returnValue(false))
;
$handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
$handler2->expects($this->once())
->method('isHandling')
->will($this->returnValue(true))
;
$handler2->expects($this->once())
->method('handle')
->will($this->returnValue(false))
;
$handler3 = $this->getMock('Monolog\Handler\HandlerInterface');
$handler3->expects($this->once())
->method('isHandling')
->will($this->returnValue(false))
;
$handler3->expects($this->never())
->method('handle')
;
$logger = new Logger(__METHOD__, array('last' => $handler3, 'second' => $handler2, 'first' => $handler1));
$logger->debug('test');
}
/** /**
* @covers Monolog\Logger::addRecord * @covers Monolog\Logger::addRecord
*/ */