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

Add a way to disable logging loop detection, closes #1681

This commit is contained in:
Jordi Boggiano
2022-06-08 21:51:47 +02:00
parent 247918972a
commit 9c1566a971

View File

@@ -152,6 +152,13 @@ class Logger implements LoggerInterface, ResettableInterface
*/ */
private $logDepth = 0; 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<callable(array): array> $processors * @psalm-param array<callable(array): array> $processors
* *
@@ -284,6 +291,13 @@ class Logger implements LoggerInterface, ResettableInterface
return $this; return $this;
} }
public function useLoggingLoopDetection(bool $detectCycles): self
{
$this->detectCycles = $detectCycles;
return $this;
}
/** /**
* Adds a log record. * Adds a log record.
* *
@@ -296,7 +310,9 @@ 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
{ {
$this->logDepth += 1; if ($this->detectCycles) {
$this->logDepth += 1;
}
if ($this->logDepth === 3) { 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.'); $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; return false;
@@ -349,7 +365,9 @@ class Logger implements LoggerInterface, ResettableInterface
} }
} }
} finally { } finally {
$this->logDepth--; if ($this->detectCycles) {
$this->logDepth--;
}
} }
return null !== $record; return null !== $record;