mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-01 19:00:20 +02:00
Add Level::toRFC5424Level method and restore BC to Syslog handlers (broken in #1689)
This commit is contained in:
@@ -56,12 +56,12 @@ class SyslogFormatter extends LineFormatter
|
||||
$extra['procid'] = $this->procid;
|
||||
$extra['priority'] = self::calculatePriority($record->level);
|
||||
$extra['structured-data'] = self::NILVALUE;
|
||||
|
||||
|
||||
return $extra;
|
||||
}
|
||||
|
||||
private static function calculatePriority(Level $level): int
|
||||
{
|
||||
return (self::SYSLOG_FACILITY_USER * 8) + SyslogUtils::toSyslogPriority($level);
|
||||
return (self::SYSLOG_FACILITY_USER * 8) + $level->toRFC5424Level();
|
||||
}
|
||||
}
|
||||
|
@@ -40,6 +40,14 @@ abstract class AbstractSyslogHandler extends AbstractProcessingHandler
|
||||
'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
|
||||
*/
|
||||
|
@@ -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,
|
||||
};
|
||||
}
|
||||
}
|
@@ -62,6 +62,6 @@ class SyslogHandler extends AbstractSyslogHandler
|
||||
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));
|
||||
}
|
||||
syslog(SyslogUtils::toSyslogPriority($record->level), (string) $record->formatted);
|
||||
syslog($this->toSyslogPriority($record->level), (string) $record->formatted);
|
||||
}
|
||||
}
|
||||
|
@@ -9,7 +9,7 @@
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Monolog\Handler\Syslog\SyslogUdp;
|
||||
namespace Monolog\Handler\SyslogUdp;
|
||||
|
||||
use Monolog\Utils;
|
||||
use Socket;
|
@@ -12,11 +12,11 @@
|
||||
namespace Monolog\Handler;
|
||||
|
||||
use DateTimeInterface;
|
||||
use Monolog\Level;
|
||||
use Monolog\Handler\Syslog\SyslogUdp\UdpSocket;
|
||||
use Monolog\Handler\Syslog\SyslogUtils;
|
||||
use Monolog\Utils;
|
||||
use Monolog\Handler\SyslogUdp\UdpSocket;
|
||||
use Monolog\Level;
|
||||
use Monolog\LogRecord;
|
||||
use Monolog\Utils;
|
||||
|
||||
/**
|
||||
* A Handler for logging to a remote syslogd server.
|
||||
@@ -71,7 +71,7 @@ class SyslogUdpHandler extends AbstractSyslogHandler
|
||||
{
|
||||
$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) {
|
||||
$this->socket->write($line, $header);
|
||||
|
@@ -23,6 +23,7 @@ use Psr\Log\LogLevel;
|
||||
*
|
||||
* - 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 ->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")
|
||||
*
|
||||
* 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::*
|
||||
*/
|
||||
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 = [
|
||||
100,
|
||||
200,
|
||||
|
@@ -35,7 +35,7 @@ class SyslogUdpHandlerTest extends TestCase
|
||||
$handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
|
||||
|
||||
$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'])
|
||||
->setConstructorArgs(['lol'])
|
||||
->getMock();
|
||||
@@ -56,7 +56,7 @@ class SyslogUdpHandlerTest extends TestCase
|
||||
$handler = new SyslogUdpHandler("127.0.0.1", 514, "authpriv");
|
||||
$handler->setFormatter($this->getIdentityFormatter());
|
||||
|
||||
$socket = $this->getMockBuilder('Monolog\Handler\Syslog\SyslogUdp\UdpSocket')
|
||||
$socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
|
||||
->onlyMethods(['write'])
|
||||
->setConstructorArgs(['lol'])
|
||||
->getMock();
|
||||
@@ -81,7 +81,7 @@ class SyslogUdpHandlerTest extends TestCase
|
||||
|
||||
$handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
|
||||
|
||||
$socket = $this->getMockBuilder('\Monolog\Handler\Syslog\SyslogUdp\UdpSocket')
|
||||
$socket = $this->getMockBuilder('\Monolog\Handler\SyslogUdp\UdpSocket')
|
||||
->setConstructorArgs(['lol', 999])
|
||||
->onlyMethods(['write'])
|
||||
->getMock();
|
||||
|
@@ -11,8 +11,8 @@
|
||||
|
||||
namespace Monolog\Handler;
|
||||
|
||||
use Monolog\Handler\SyslogUdp\UdpSocket;
|
||||
use Monolog\Test\TestCase;
|
||||
use Monolog\Handler\Syslog\SyslogUdp\UdpSocket;
|
||||
|
||||
/**
|
||||
* @requires extension sockets
|
||||
@@ -21,7 +21,7 @@ class UdpSocketTest extends TestCase
|
||||
{
|
||||
public function testWeDoNotTruncateShortMessages()
|
||||
{
|
||||
$socket = $this->getMockBuilder('Monolog\Handler\Syslog\SyslogUdp\UdpSocket')
|
||||
$socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
|
||||
->onlyMethods(['send'])
|
||||
->setConstructorArgs(['lol'])
|
||||
->getMock();
|
||||
@@ -35,7 +35,7 @@ class UdpSocketTest extends TestCase
|
||||
|
||||
public function testLongMessagesAreTruncated()
|
||||
{
|
||||
$socket = $this->getMockBuilder('Monolog\Handler\Syslog\SyslogUdp\UdpSocket')
|
||||
$socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
|
||||
->onlyMethods(['send'])
|
||||
->setConstructorArgs(['lol'])
|
||||
->getMock();
|
||||
|
Reference in New Issue
Block a user