mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-07 21:56:31 +02:00
Simplify addRecord further by doing a single foreach over all handlers, refs #1489
This commit is contained in:
@@ -286,18 +286,13 @@ class Logger implements LoggerInterface, ResettableInterface
|
||||
public function addRecord(int $level, string $message, array $context = []): bool
|
||||
{
|
||||
$offset = 0;
|
||||
$record = null;
|
||||
|
||||
foreach ($this->handlers as $handler) {
|
||||
if ($handler->isHandling(['level' => $level])) {
|
||||
break;
|
||||
}
|
||||
|
||||
$offset++;
|
||||
}
|
||||
// cut off checked not handleable handlers
|
||||
$remainedHandlers = array_slice($this->handlers, $offset);
|
||||
|
||||
if (!$remainedHandlers) {
|
||||
return false;
|
||||
if (null === $record) {
|
||||
// skip creating the record as long as no handler is going to handle it
|
||||
if (!$handler->isHandling(['level' => $level])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$levelName = static::getLevelName($level);
|
||||
@@ -316,18 +311,29 @@ class Logger implements LoggerInterface, ResettableInterface
|
||||
foreach ($this->processors as $processor) {
|
||||
$record = $processor($record);
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
$this->handleException($e, $record);
|
||||
|
||||
foreach ($remainedHandlers as $handler) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// once the record exist, send it to all handlers as long as the bubbling chain is not interrupted
|
||||
if (null !== $record) {
|
||||
try {
|
||||
if (true === $handler->handle($record)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (Throwable $e) {
|
||||
$this->handleException($e, $record);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null !== $record;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends a log cycle and frees all resources used by handlers.
|
||||
|
Reference in New Issue
Block a user