From aa6e88b6de881647f4c94a055fa024d774e172dc Mon Sep 17 00:00:00 2001 From: ont Date: Wed, 15 Mar 2017 00:57:06 +0700 Subject: [PATCH 1/4] Complete rfc5424 header for SyslogUdpHandler WARN: this commit adds backward incompatibility for the SyslogUdpHandler constructor. --- src/Monolog/Handler/SyslogUdpHandler.php | 23 +++++++++++++++++-- .../Monolog/Handler/SyslogUdpHandlerTest.php | 19 +++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/Monolog/Handler/SyslogUdpHandler.php b/src/Monolog/Handler/SyslogUdpHandler.php index 74d946a5..fb4dac8c 100644 --- a/src/Monolog/Handler/SyslogUdpHandler.php +++ b/src/Monolog/Handler/SyslogUdpHandler.php @@ -22,18 +22,22 @@ use Monolog\Handler\SyslogUdp\UdpSocket; class SyslogUdpHandler extends AbstractSyslogHandler { protected $socket; + protected $ident; /** * @param string $host * @param int $port + * @param string $ident Program name or tag for each log message. * @param mixed $facility * @param int $level The minimum logging level at which this handler will be triggered * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not */ - public function __construct($host, $port = 514, $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true) + public function __construct($host, $port = 514, $ident = 'php', $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true) { parent::__construct($facility, $level, $bubble); + $this->ident = $ident; + $this->socket = new UdpSocket($host, $port ?: 514); } @@ -69,7 +73,22 @@ class SyslogUdpHandler extends AbstractSyslogHandler { $priority = $severity + $this->facility; - return "<$priority>1 "; + if(!$pid = getmypid()) + $pid = '-'; + + if(!$hostname = gethostname()) + $hostname = '-'; + + return "<$priority>1 ". + $this->getDateTime()." ". + $hostname." ". + $this->ident." ". + $pid." - - "; + } + + protected function getDateTime() + { + return date(\DateTime::RFC3339); } /** diff --git a/tests/Monolog/Handler/SyslogUdpHandlerTest.php b/tests/Monolog/Handler/SyslogUdpHandlerTest.php index aa4a3341..9e5fab14 100644 --- a/tests/Monolog/Handler/SyslogUdpHandlerTest.php +++ b/tests/Monolog/Handler/SyslogUdpHandlerTest.php @@ -23,21 +23,32 @@ class SyslogUdpHandlerTest extends TestCase */ public function testWeValidateFacilities() { - $handler = new SyslogUdpHandler("ip", null, "invalidFacility"); + $handler = new SyslogUdpHandler("ip", null, "php", "invalidFacility"); } public function testWeSplitIntoLines() { - $handler = new SyslogUdpHandler("127.0.0.1", 514, "authpriv"); + $time = '2014-01-07T12:34'; + $pid = getmypid(); + $host = gethostname(); + + $handler = $this->getMockBuilder('\Monolog\Handler\SyslogUdpHandler') + ->setConstructorArgs(["127.0.0.1", 514, "php", "authpriv"]) + ->setMethods(['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).">1 "); + ->with("lol", "<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - "); $socket->expects($this->at(1)) ->method('write') - ->with("hej", "<".(LOG_AUTHPRIV + LOG_WARNING).">1 "); + ->with("hej", "<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - "); $handler->setSocket($socket); From 8c40211c3f321516d0a1cfd972f027e56b1453bd Mon Sep 17 00:00:00 2001 From: ont Date: Wed, 15 Mar 2017 12:00:44 +0700 Subject: [PATCH 2/4] Fix array syntax for Travis CI --- tests/Monolog/Handler/SyslogUdpHandlerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Monolog/Handler/SyslogUdpHandlerTest.php b/tests/Monolog/Handler/SyslogUdpHandlerTest.php index 9e5fab14..d58e5dcb 100644 --- a/tests/Monolog/Handler/SyslogUdpHandlerTest.php +++ b/tests/Monolog/Handler/SyslogUdpHandlerTest.php @@ -33,8 +33,8 @@ class SyslogUdpHandlerTest extends TestCase $host = gethostname(); $handler = $this->getMockBuilder('\Monolog\Handler\SyslogUdpHandler') - ->setConstructorArgs(["127.0.0.1", 514, "php", "authpriv"]) - ->setMethods(['getDateTime']) + ->setConstructorArgs(array("127.0.0.1", 514, "php", "authpriv")) + ->setMethods(array('getDateTime')) ->getMock(); $handler->method('getDateTime') From aeb65fa0504282cff0a07094631a0cd86e9fcc10 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 17 Mar 2017 22:52:50 +0100 Subject: [PATCH 3/4] Fix BC break and formatting --- src/Monolog/Handler/SyslogUdpHandler.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Monolog/Handler/SyslogUdpHandler.php b/src/Monolog/Handler/SyslogUdpHandler.php index fb4dac8c..4718711b 100644 --- a/src/Monolog/Handler/SyslogUdpHandler.php +++ b/src/Monolog/Handler/SyslogUdpHandler.php @@ -27,12 +27,12 @@ class SyslogUdpHandler extends AbstractSyslogHandler /** * @param string $host * @param int $port - * @param string $ident Program name or tag for each log message. * @param mixed $facility * @param int $level The minimum logging level at which this handler will be triggered * @param Boolean $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. */ - public function __construct($host, $port = 514, $ident = 'php', $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true) + public function __construct($host, $port = 514, $facility = LOG_USER, $level = Logger::DEBUG, $bubble = true, $ident = 'php') { parent::__construct($facility, $level, $bubble); @@ -73,17 +73,19 @@ class SyslogUdpHandler extends AbstractSyslogHandler { $priority = $severity + $this->facility; - if(!$pid = getmypid()) + if (!$pid = getmypid()) { $pid = '-'; + } - if(!$hostname = gethostname()) + if (!$hostname = gethostname()) { $hostname = '-'; + } - return "<$priority>1 ". - $this->getDateTime()." ". - $hostname." ". - $this->ident." ". - $pid." - - "; + return "<$priority>1 " . + $this->getDateTime() . " " . + $hostname . " " . + $this->ident . " " . + $pid . " - - "; } protected function getDateTime() From 24894709d1732ea8783c7ddfd8bca0bd7222f2c4 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 17 Mar 2017 22:54:56 +0100 Subject: [PATCH 4/4] Fix tests --- tests/Monolog/Handler/SyslogUdpHandlerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Monolog/Handler/SyslogUdpHandlerTest.php b/tests/Monolog/Handler/SyslogUdpHandlerTest.php index d58e5dcb..7ee8a985 100644 --- a/tests/Monolog/Handler/SyslogUdpHandlerTest.php +++ b/tests/Monolog/Handler/SyslogUdpHandlerTest.php @@ -23,7 +23,7 @@ class SyslogUdpHandlerTest extends TestCase */ public function testWeValidateFacilities() { - $handler = new SyslogUdpHandler("ip", null, "php", "invalidFacility"); + $handler = new SyslogUdpHandler("ip", null, "invalidFacility"); } public function testWeSplitIntoLines() @@ -33,7 +33,7 @@ class SyslogUdpHandlerTest extends TestCase $host = gethostname(); $handler = $this->getMockBuilder('\Monolog\Handler\SyslogUdpHandler') - ->setConstructorArgs(array("127.0.0.1", 514, "php", "authpriv")) + ->setConstructorArgs(array("127.0.0.1", 514, "authpriv")) ->setMethods(array('getDateTime')) ->getMock();