From 80e8b0d575b169d2065d95992f77bf29e272fd8d Mon Sep 17 00:00:00 2001 From: Dominik Kukacka Date: Fri, 22 Mar 2019 22:40:59 +0100 Subject: [PATCH] Add possibility to use RFC3164 for udp syslog --- src/Monolog/Handler/SyslogUdpHandler.php | 37 +++++++++++++++---- .../Monolog/Handler/SyslogUdpHandlerTest.php | 30 +++++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/src/Monolog/Handler/SyslogUdpHandler.php b/src/Monolog/Handler/SyslogUdpHandler.php index e14b378c..4dfd5f5e 100644 --- a/src/Monolog/Handler/SyslogUdpHandler.php +++ b/src/Monolog/Handler/SyslogUdpHandler.php @@ -18,11 +18,21 @@ use Monolog\Handler\SyslogUdp\UdpSocket; * A Handler for logging to a remote syslogd server. * * @author Jesper Skovgaard Nielsen + * @author Dominik Kukacka */ class SyslogUdpHandler extends AbstractSyslogHandler { + const RFC3164 = 0; + const RFC5424 = 1; + + private $dateFormats = array( + self::RFC3164 => 'M d H:i:s', + self::RFC5424 => \DateTime::RFC3339, + ); + protected $socket; protected $ident; + protected $rfc; /** * @param string $host @@ -31,12 +41,14 @@ class SyslogUdpHandler extends AbstractSyslogHandler * @param int $level The minimum logging level at which this handler will be triggered * @param bool $bubble Whether the messages that are handled can bubble up the stack or not * @param string $ident Program name or tag for each log message. + * @param int $rfc RFC to format the message for. */ - public function __construct($host, $port = 514, $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true, $ident = 'php') + public function __construct($host, $port = 514, $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true, $ident = 'php', $rfc = self::RFC5424) { parent::__construct($facility, $level, $bubble); $this->ident = $ident; + $this->rfc = $rfc; $this->socket = new UdpSocket($host, $port ?: 514); } @@ -67,7 +79,7 @@ class SyslogUdpHandler extends AbstractSyslogHandler } /** - * Make common syslog header (see rfc5424) + * Make common syslog header (see rfc5424 or rfc3164) */ protected function makeCommonSyslogHeader($severity) { @@ -81,16 +93,25 @@ class SyslogUdpHandler extends AbstractSyslogHandler $hostname = '-'; } - return "<$priority>1 " . - $this->getDateTime() . " " . - $hostname . " " . - $this->ident . " " . - $pid . " - - "; + $date = $this->getDateTime(); + + if ($this->rfc === self::RFC3164) { + return "<$priority>" . + $date . " " . + $hostname . " " . + $this->ident . "[" . $pid . "]: "; + } else { + return "<$priority>1 " . + $date . " " . + $hostname . " " . + $this->ident . " " . + $pid . " - - "; + } } protected function getDateTime() { - return date(\DateTime::RFC3339); + return date($this->dateFormats[$this->rfc]); } /** diff --git a/tests/Monolog/Handler/SyslogUdpHandlerTest.php b/tests/Monolog/Handler/SyslogUdpHandlerTest.php index 7ee8a985..a1ea9d03 100644 --- a/tests/Monolog/Handler/SyslogUdpHandlerTest.php +++ b/tests/Monolog/Handler/SyslogUdpHandlerTest.php @@ -69,6 +69,36 @@ class SyslogUdpHandlerTest extends TestCase $handler->handle($this->getRecordWithMessage(null)); } + + public function testRfc() + { + $time = 'Mar 22 21:16:47'; + $pid = getmypid(); + $host = gethostname(); + + $handler = $this->getMockBuilder('\Monolog\Handler\SyslogUdpHandler') + ->setConstructorArgs(array("127.0.0.1", 514, "authpriv", null, null, "php", \Monolog\Handler\SyslogUdpHandler::RFC3164)) + ->setMethods(array('getDateTime')) + ->getMock(); + + $handler->method('getDateTime') + ->willReturn($time); + + $handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter()); + + $socket = $this->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', array('write'), array('lol', 'lol')); + $socket->expects($this->at(0)) + ->method('write') + ->with("lol", "<".(LOG_AUTHPRIV + LOG_WARNING).">$time $host php[$pid]: "); + $socket->expects($this->at(1)) + ->method('write') + ->with("hej", "<".(LOG_AUTHPRIV + LOG_WARNING).">$time $host php[$pid]: "); + + $handler->setSocket($socket); + + $handler->handle($this->getRecordWithMessage("hej\nlol")); + } + protected function getRecordWithMessage($msg) { return array('message' => $msg, 'level' => \Monolog\Logger::WARNING, 'context' => null, 'extra' => array(), 'channel' => 'lol');