From 9c1566a971d42ff1acb4756708e8d9a5568bb194 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Wed, 8 Jun 2022 21:51:47 +0200 Subject: [PATCH] Add a way to disable logging loop detection, closes #1681 --- src/Monolog/Logger.php | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 0c2240e2..04118959 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -152,6 +152,13 @@ class Logger implements LoggerInterface, ResettableInterface */ private $logDepth = 0; + /** + * @var bool Whether to detect infinite logging loops + * + * This can be disabled via {@see useLoggingLoopDetection} if you have async handlers that do not play well with this + */ + private $detectCycles = true; + /** * @psalm-param array $processors * @@ -284,6 +291,13 @@ class Logger implements LoggerInterface, ResettableInterface return $this; } + public function useLoggingLoopDetection(bool $detectCycles): self + { + $this->detectCycles = $detectCycles; + + return $this; + } + /** * Adds a log record. * @@ -296,7 +310,9 @@ class Logger implements LoggerInterface, ResettableInterface */ public function addRecord(int $level, string $message, array $context = []): bool { - $this->logDepth += 1; + if ($this->detectCycles) { + $this->logDepth += 1; + } if ($this->logDepth === 3) { $this->warning('A possible infinite logging loop was detected and aborted. It appears some of your handler code is triggering logging, see the previous log record for a hint as to what may be the cause.'); return false; @@ -349,7 +365,9 @@ class Logger implements LoggerInterface, ResettableInterface } } } finally { - $this->logDepth--; + if ($this->detectCycles) { + $this->logDepth--; + } } return null !== $record;