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

Fix infinite loops when a log handler triggers logging itself

This commit is contained in:
Jordi Boggiano
2022-05-10 11:10:07 +02:00
parent 5d43fd52d3
commit a5d65f6ec4
2 changed files with 104 additions and 31 deletions

View File

@@ -147,6 +147,11 @@ class Logger implements LoggerInterface, ResettableInterface
*/
protected $exceptionHandler;
/**
* @var int Keeps track of depth to prevent infinite logging loops
*/
private $logDepth = 0;
/**
* @psalm-param array<callable(array): array> $processors
*
@@ -291,6 +296,15 @@ class Logger implements LoggerInterface, ResettableInterface
*/
public function addRecord(int $level, string $message, array $context = []): bool
{
$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;
} elseif ($this->logDepth >= 5) { // log depth 4 is let through so we can log the warning above
return false;
}
try {
$record = null;
foreach ($this->handlers as $handler) {
@@ -334,6 +348,9 @@ class Logger implements LoggerInterface, ResettableInterface
return true;
}
}
} finally {
$this->logDepth--;
}
return null !== $record;
}

View File

@@ -11,6 +11,7 @@
namespace Monolog;
use Monolog\Handler\HandlerInterface;
use Monolog\Processor\WebProcessor;
use Monolog\Handler\TestHandler;
@@ -84,6 +85,28 @@ class LoggerTest extends \PHPUnit\Framework\TestCase
$this->assertEquals('foo', $record['channel']);
}
/**
* @covers Monolog\Logger::addRecord
*/
public function testLogPreventsCircularLogging()
{
$logger = new Logger(__METHOD__);
$loggingHandler = new LoggingHandler($logger);
$testHandler = new TestHandler();
$logger->pushHandler($loggingHandler);
$logger->pushHandler($testHandler);
$logger->addRecord(Logger::ALERT, 'test');
$records = $testHandler->getRecords();
$this->assertCount(3, $records);
$this->assertSame('ALERT', $records[0]['level_name']);
$this->assertSame('DEBUG', $records[1]['level_name']);
$this->assertSame('WARNING', $records[2]['level_name']);
}
/**
* @covers Monolog\Logger::addRecord
*/
@@ -717,3 +740,36 @@ class LoggerTest extends \PHPUnit\Framework\TestCase
$this->assertNotSame($uid2, $processorUid2->getUid());
}
}
class LoggingHandler implements HandlerInterface
{
/**
* @var Logger
*/
private $logger;
public function __construct(Logger $logger)
{
$this->logger = $logger;
}
public function isHandling(array $record): bool
{
return true;
}
public function handle(array $record): bool
{
$this->logger->debug('Log triggered while logging');
return false;
}
public function handleBatch(array $records): void
{
}
public function close(): void
{
}
}