Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1686 - Add RFC 5424 support #1723

Merged
merged 2 commits into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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