Skip to content

Commit cab3ff7

Browse files
committed
Add regression test for Seldaek#691 and optimize implementation using a single pass loop over the handlers, refs Seldaek#692
1 parent d4ae6cc commit cab3ff7

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

Diff for: src/Monolog/Logger.php

+9-10
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,14 @@ public function addRecord($level, $message, array $context = array())
280280

281281
// check if any handler will handle this message so we can return early and save cycles
282282
$handlerKey = null;
283-
foreach ($this->handlers as $key => $handler) {
283+
reset($this->handlers);
284+
while ($handler = current($this->handlers)) {
284285
if ($handler->isHandling(array('level' => $level))) {
285-
$handlerKey = $key;
286+
$handlerKey = key($this->handlers);
286287
break;
287288
}
289+
290+
next($this->handlers);
288291
}
289292

290293
if (null === $handlerKey) {
@@ -315,17 +318,13 @@ public function addRecord($level, $message, array $context = array())
315318
foreach ($this->processors as $processor) {
316319
$record = call_user_func($processor, $record);
317320
}
318-
$foundStartingKey = false;
319-
foreach ($this->handlers as $key => $handler) {
320-
if ($key === $handlerKey) {
321-
$foundStartingKey = true;
322-
}
323-
if ($foundStartingKey === false) {
324-
continue;
325-
}
321+
322+
while ($handler = current($this->handlers)) {
326323
if (true === $handler->handle($record)) {
327324
break;
328325
}
326+
327+
next($this->handlers);
329328
}
330329

331330
return true;

Diff for: tests/Monolog/LoggerTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,45 @@ public function testHandlersNotCalledBeforeFirstHandling()
303303
$logger->debug('test');
304304
}
305305

306+
/**
307+
* @covers Monolog\Logger::addRecord
308+
*/
309+
public function testHandlersNotCalledBeforeFirstHandlingWithAssocArray()
310+
{
311+
$handler1 = $this->getMock('Monolog\Handler\HandlerInterface');
312+
$handler1->expects($this->never())
313+
->method('isHandling')
314+
->will($this->returnValue(false))
315+
;
316+
$handler1->expects($this->once())
317+
->method('handle')
318+
->will($this->returnValue(false))
319+
;
320+
321+
$handler2 = $this->getMock('Monolog\Handler\HandlerInterface');
322+
$handler2->expects($this->once())
323+
->method('isHandling')
324+
->will($this->returnValue(true))
325+
;
326+
$handler2->expects($this->once())
327+
->method('handle')
328+
->will($this->returnValue(false))
329+
;
330+
331+
$handler3 = $this->getMock('Monolog\Handler\HandlerInterface');
332+
$handler3->expects($this->once())
333+
->method('isHandling')
334+
->will($this->returnValue(false))
335+
;
336+
$handler3->expects($this->never())
337+
->method('handle')
338+
;
339+
340+
$logger = new Logger(__METHOD__, array('last' => $handler3, 'second' => $handler2, 'first' => $handler1));
341+
342+
$logger->debug('test');
343+
}
344+
306345
/**
307346
* @covers Monolog\Logger::addRecord
308347
*/

0 commit comments

Comments
 (0)