diff --git a/src/Monolog/Logger.php b/src/Monolog/Logger.php index 099185205..1ab75b9e0 100644 --- a/src/Monolog/Logger.php +++ b/src/Monolog/Logger.php @@ -111,6 +111,22 @@ class Logger implements LoggerInterface, ResettableInterface self::EMERGENCY => 'EMERGENCY', ]; + /** + * Mapping between levels numbers defined in RFC 5424 and Monolog ones + * + * @phpstan-var array $rfc_5424_levels + */ + private const RFC_5424_LEVELS = [ + 7 => self::DEBUG, + 6 => self::INFO, + 5 => self::NOTICE, + 4 => self::WARNING, + 3 => self::ERROR, + 2 => self::CRITICAL, + 1 => self::ALERT, + 0 => self::EMERGENCY, + ]; + /** * @var string */ @@ -301,7 +317,7 @@ public function useLoggingLoopDetection(bool $detectCycles): self /** * Adds a log record. * - * @param int $level The logging level + * @param int $level The logging level (a Monolog or RFC 5424 level) * @param string $message The log message * @param mixed[] $context The log context * @param DateTimeImmutable $datetime Optional log date to log into the past or future @@ -311,6 +327,10 @@ public function useLoggingLoopDetection(bool $detectCycles): self */ public function addRecord(int $level, string $message, array $context = [], DateTimeImmutable $datetime = null): bool { + if (isset(self::RFC_5424_LEVELS[$level])) { + $level = self::RFC_5424_LEVELS[$level]; + } + if ($this->detectCycles) { $this->logDepth += 1; } @@ -520,7 +540,7 @@ public function getExceptionHandler(): ?callable * * This method allows for compatibility with common interfaces. * - * @param mixed $level The log level + * @param mixed $level The log level (a Monolog, PSR-3 or RFC 5424 level) * @param string|Stringable $message The log message * @param mixed[] $context The log context * @@ -532,6 +552,10 @@ public function log($level, $message, array $context = []): void throw new \InvalidArgumentException('$level is expected to be a string or int'); } + if (isset(self::RFC_5424_LEVELS[$level])) { + $level = self::RFC_5424_LEVELS[$level]; + } + $level = static::toMonologLevel($level); $this->addRecord($level, (string) $message, $context); diff --git a/tests/Monolog/LoggerTest.php b/tests/Monolog/LoggerTest.php index 0b07a7cf1..fd99df206 100644 --- a/tests/Monolog/LoggerTest.php +++ b/tests/Monolog/LoggerTest.php @@ -62,6 +62,37 @@ public function testConvertPSR3ToMonologLevel() $this->assertEquals(Logger::toMonologLevel('emergency'), 600); } + /** + * @covers Monolog\Logger::addRecord + * @covers Monolog\Logger::log + */ + public function testConvertRFC5424ToMonologLevelInAddRecordAndLog() + { + $logger = new Logger('test'); + $handler = new TestHandler; + $logger->pushHandler($handler); + + foreach ([ + 7 => 100, + 6 => 200, + 5 => 250, + 4 => 300, + 3 => 400, + 2 => 500, + 1 => 550, + 0 => 600, + ] as $rfc5424Level => $monologLevel) { + $handler->reset(); + $logger->addRecord($rfc5424Level, 'test'); + $logger->log($rfc5424Level, 'test'); + $records = $handler->getRecords(); + + self::assertCount(2, $records); + self::assertSame($monologLevel, $records[0]['level']); + self::assertSame($monologLevel, $records[1]['level']); + } + } + /** * @covers Monolog\Logger::getLevelName */