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

Simplify addRecord further by doing a single foreach over all handlers, refs #1489

This commit is contained in:
Jordi Boggiano
2020-12-10 14:30:02 +01:00
parent c203cf84b5
commit df4d93f148

View File

@@ -286,47 +286,53 @@ class Logger implements LoggerInterface, ResettableInterface
public function addRecord(int $level, string $message, array $context = []): bool public function addRecord(int $level, string $message, array $context = []): bool
{ {
$offset = 0; $offset = 0;
$record = null;
foreach ($this->handlers as $handler) { foreach ($this->handlers as $handler) {
if ($handler->isHandling(['level' => $level])) { if (null === $record) {
break; // skip creating the record as long as no handler is going to handle it
} if (!$handler->isHandling(['level' => $level])) {
continue;
}
$offset++; $levelName = static::getLevelName($level);
}
// cut off checked not handleable handlers
$remainedHandlers = array_slice($this->handlers, $offset);
if (!$remainedHandlers) { $record = [
return false; 'message' => $message,
} 'context' => $context,
'level' => $level,
'level_name' => $levelName,
'channel' => $this->name,
'datetime' => new DateTimeImmutable($this->microsecondTimestamps, $this->timezone),
'extra' => [],
];
$levelName = static::getLevelName($level); try {
foreach ($this->processors as $processor) {
$record = $processor($record);
}
} catch (Throwable $e) {
$this->handleException($e, $record);
$record = [ return true;
'message' => $message, }
'context' => $context, }
'level' => $level,
'level_name' => $levelName, // once the record exist, send it to all handlers as long as the bubbling chain is not interrupted
'channel' => $this->name, if (null !== $record) {
'datetime' => new DateTimeImmutable($this->microsecondTimestamps, $this->timezone), try {
'extra' => [], if (true === $handler->handle($record)) {
]; break;
}
try { } catch (Throwable $e) {
foreach ($this->processors as $processor) { $this->handleException($e, $record);
$record = $processor($record);
} return true;
foreach ($remainedHandlers as $handler) {
if (true === $handler->handle($record)) {
break;
} }
} }
} catch (Throwable $e) {
$this->handleException($e, $record);
} }
return true; return null !== $record;
} }
/** /**
@@ -407,7 +413,7 @@ class Logger implements LoggerInterface, ResettableInterface
if (is_numeric($level)) { if (is_numeric($level)) {
return intval($level); return intval($level);
} }
// Contains chars of all log levels and avoids using strtoupper() which may have // Contains chars of all log levels and avoids using strtoupper() which may have
// strange results depending on locale (for example, "i" will become "İ" in Turkish locale) // strange results depending on locale (for example, "i" will become "İ" in Turkish locale)
$upper = strtr($level, 'abcdefgilmnortuwy', 'ABCDEFGILMNORTUWY'); $upper = strtr($level, 'abcdefgilmnortuwy', 'ABCDEFGILMNORTUWY');