1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-02 19:27:37 +02:00

Add Level::toRFC5424Level method and restore BC to Syslog handlers (broken in #1689)

This commit is contained in:
Jordi Boggiano
2022-07-22 17:04:34 +02:00
parent 7d31b642bd
commit f7dfa00f08
9 changed files with 44 additions and 36 deletions

View File

@@ -56,12 +56,12 @@ class SyslogFormatter extends LineFormatter
$extra['procid'] = $this->procid; $extra['procid'] = $this->procid;
$extra['priority'] = self::calculatePriority($record->level); $extra['priority'] = self::calculatePriority($record->level);
$extra['structured-data'] = self::NILVALUE; $extra['structured-data'] = self::NILVALUE;
return $extra; return $extra;
} }
private static function calculatePriority(Level $level): int private static function calculatePriority(Level $level): int
{ {
return (self::SYSLOG_FACILITY_USER * 8) + SyslogUtils::toSyslogPriority($level); return (self::SYSLOG_FACILITY_USER * 8) + $level->toRFC5424Level();
} }
} }

View File

@@ -40,6 +40,14 @@ abstract class AbstractSyslogHandler extends AbstractProcessingHandler
'uucp' => \LOG_UUCP, 'uucp' => \LOG_UUCP,
]; ];
/**
* Translates Monolog log levels to syslog log priorities.
*/
protected function toSyslogPriority(Level $level): int
{
return $level->toRFC5424Level();
}
/** /**
* @param string|int $facility Either one of the names of the keys in $this->facilities, or a LOG_* facility constant * @param string|int $facility Either one of the names of the keys in $this->facilities, or a LOG_* facility constant
*/ */

View File

@@ -1,22 +0,0 @@
<?php
namespace Monolog\Handler\Syslog;
use Monolog\Level;
class SyslogUtils
{
public static function toSyslogPriority(Level $level): int
{
return match ($level) {
Level::Debug => \LOG_DEBUG,
Level::Info => \LOG_INFO,
Level::Notice => \LOG_NOTICE,
Level::Warning => \LOG_WARNING,
Level::Error => \LOG_ERR,
Level::Critical => \LOG_CRIT,
Level::Alert => \LOG_ALERT,
Level::Emergency => \LOG_EMERG,
};
}
}

View File

@@ -62,6 +62,6 @@ class SyslogHandler extends AbstractSyslogHandler
if (!openlog($this->ident, $this->logopts, $this->facility)) { if (!openlog($this->ident, $this->logopts, $this->facility)) {
throw new \LogicException('Can\'t open syslog for ident "'.$this->ident.'" and facility "'.$this->facility.'"' . Utils::getRecordMessageForException($record)); throw new \LogicException('Can\'t open syslog for ident "'.$this->ident.'" and facility "'.$this->facility.'"' . Utils::getRecordMessageForException($record));
} }
syslog(SyslogUtils::toSyslogPriority($record->level), (string) $record->formatted); syslog($this->toSyslogPriority($record->level), (string) $record->formatted);
} }
} }

View File

@@ -9,7 +9,7 @@
* file that was distributed with this source code. * file that was distributed with this source code.
*/ */
namespace Monolog\Handler\Syslog\SyslogUdp; namespace Monolog\Handler\SyslogUdp;
use Monolog\Utils; use Monolog\Utils;
use Socket; use Socket;

View File

@@ -12,11 +12,11 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use DateTimeInterface; use DateTimeInterface;
use Monolog\Level;
use Monolog\Handler\Syslog\SyslogUdp\UdpSocket;
use Monolog\Handler\Syslog\SyslogUtils; use Monolog\Handler\Syslog\SyslogUtils;
use Monolog\Utils; use Monolog\Handler\SyslogUdp\UdpSocket;
use Monolog\Level;
use Monolog\LogRecord; use Monolog\LogRecord;
use Monolog\Utils;
/** /**
* A Handler for logging to a remote syslogd server. * A Handler for logging to a remote syslogd server.
@@ -71,7 +71,7 @@ class SyslogUdpHandler extends AbstractSyslogHandler
{ {
$lines = $this->splitMessageIntoLines($record->formatted); $lines = $this->splitMessageIntoLines($record->formatted);
$header = $this->makeCommonSyslogHeader(SyslogUtils::toSyslogPriority($record->level), $record->datetime); $header = $this->makeCommonSyslogHeader($this->toSyslogPriority($record->level), $record->datetime);
foreach ($lines as $line) { foreach ($lines as $line) {
$this->socket->write($line, $header); $this->socket->write($line, $header);

View File

@@ -23,6 +23,7 @@ use Psr\Log\LogLevel;
* *
* - Use ->getName() to get the standard Monolog name which is full uppercased (e.g. "DEBUG") * - Use ->getName() to get the standard Monolog name which is full uppercased (e.g. "DEBUG")
* - Use ->toPsrLogLevel() to get the standard PSR-3 name which is full lowercased (e.g. "debug") * - Use ->toPsrLogLevel() to get the standard PSR-3 name which is full lowercased (e.g. "debug")
* - Use ->toRFC5424Level() to get the standard RFC 5424 value (e.g. 7 for debug, 0 for emergency)
* - Use ->name to get the enum case's name which is capitalized (e.g. "Debug") * - Use ->name to get the enum case's name which is capitalized (e.g. "Debug")
* *
* To get the value for filtering, if the includes/isLowerThan/isHigherThan methods are * To get the value for filtering, if the includes/isLowerThan/isHigherThan methods are
@@ -147,6 +148,8 @@ enum Level: int
} }
/** /**
* Returns the PSR-3 level matching this instance
*
* @phpstan-return \Psr\Log\LogLevel::* * @phpstan-return \Psr\Log\LogLevel::*
*/ */
public function toPsrLogLevel(): string public function toPsrLogLevel(): string
@@ -163,6 +166,25 @@ enum Level: int
}; };
} }
/**
* Returns the RFC 5424 level matching this instance
*
* @phpstan-return int<0, 7>
*/
public function toRFC5424Level(): int
{
return match ($this) {
self::Debug => 7,
self::Info => 6,
self::Notice => 5,
self::Warning => 4,
self::Error => 3,
self::Critical => 2,
self::Alert => 1,
self::Emergency => 0,
};
}
public const VALUES = [ public const VALUES = [
100, 100,
200, 200,

View File

@@ -35,7 +35,7 @@ class SyslogUdpHandlerTest extends TestCase
$handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter()); $handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
$time = '2014-01-07T12:34:56+00:00'; $time = '2014-01-07T12:34:56+00:00';
$socket = $this->getMockBuilder('Monolog\Handler\Syslog\SyslogUdp\UdpSocket') $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
->onlyMethods(['write']) ->onlyMethods(['write'])
->setConstructorArgs(['lol']) ->setConstructorArgs(['lol'])
->getMock(); ->getMock();
@@ -56,7 +56,7 @@ class SyslogUdpHandlerTest extends TestCase
$handler = new SyslogUdpHandler("127.0.0.1", 514, "authpriv"); $handler = new SyslogUdpHandler("127.0.0.1", 514, "authpriv");
$handler->setFormatter($this->getIdentityFormatter()); $handler->setFormatter($this->getIdentityFormatter());
$socket = $this->getMockBuilder('Monolog\Handler\Syslog\SyslogUdp\UdpSocket') $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
->onlyMethods(['write']) ->onlyMethods(['write'])
->setConstructorArgs(['lol']) ->setConstructorArgs(['lol'])
->getMock(); ->getMock();
@@ -81,7 +81,7 @@ class SyslogUdpHandlerTest extends TestCase
$handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter()); $handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
$socket = $this->getMockBuilder('\Monolog\Handler\Syslog\SyslogUdp\UdpSocket') $socket = $this->getMockBuilder('\Monolog\Handler\SyslogUdp\UdpSocket')
->setConstructorArgs(['lol', 999]) ->setConstructorArgs(['lol', 999])
->onlyMethods(['write']) ->onlyMethods(['write'])
->getMock(); ->getMock();

View File

@@ -11,8 +11,8 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\Handler\SyslogUdp\UdpSocket;
use Monolog\Test\TestCase; use Monolog\Test\TestCase;
use Monolog\Handler\Syslog\SyslogUdp\UdpSocket;
/** /**
* @requires extension sockets * @requires extension sockets
@@ -21,7 +21,7 @@ class UdpSocketTest extends TestCase
{ {
public function testWeDoNotTruncateShortMessages() public function testWeDoNotTruncateShortMessages()
{ {
$socket = $this->getMockBuilder('Monolog\Handler\Syslog\SyslogUdp\UdpSocket') $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
->onlyMethods(['send']) ->onlyMethods(['send'])
->setConstructorArgs(['lol']) ->setConstructorArgs(['lol'])
->getMock(); ->getMock();
@@ -35,7 +35,7 @@ class UdpSocketTest extends TestCase
public function testLongMessagesAreTruncated() public function testLongMessagesAreTruncated()
{ {
$socket = $this->getMockBuilder('Monolog\Handler\Syslog\SyslogUdp\UdpSocket') $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
->onlyMethods(['send']) ->onlyMethods(['send'])
->setConstructorArgs(['lol']) ->setConstructorArgs(['lol'])
->getMock(); ->getMock();