Skip to content

Commit

Permalink
Add RFC 5424 level (7 to 0) support to Logger::log and Logger::addRec…
Browse files Browse the repository at this point in the history
…ord, fixes #1686 (#1723)

Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
  • Loading branch information
lussoluca and Seldaek authored Jul 22, 2022
1 parent 320909a commit ffd5055
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/Monolog/Logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int, Level> $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
*/
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down Expand Up @@ -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
*
Expand All @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions tests/Monolog/LoggerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit ffd5055

Please sign in to comment.