瀏覽代碼

Complete rfc5424 header for SyslogUdpHandler

WARN: this commit adds backward incompatibility for the SyslogUdpHandler
constructor.
ont 8 年之前
父節點
當前提交
aa6e88b6de
共有 2 個文件被更改,包括 36 次插入6 次删除
  1. 21 2
      src/Monolog/Handler/SyslogUdpHandler.php
  2. 15 4
      tests/Monolog/Handler/SyslogUdpHandlerTest.php

+ 21 - 2
src/Monolog/Handler/SyslogUdpHandler.php

@@ -22,18 +22,22 @@ use Monolog\Handler\SyslogUdp\UdpSocket;
 class SyslogUdpHandler extends AbstractSyslogHandler
 class SyslogUdpHandler extends AbstractSyslogHandler
 {
 {
     protected $socket;
     protected $socket;
+    protected $ident;
 
 
     /**
     /**
      * @param string  $host
      * @param string  $host
      * @param int     $port
      * @param int     $port
+     * @param string  $ident    Program name or tag for each log message.
      * @param mixed   $facility
      * @param mixed   $facility
      * @param int     $level    The minimum logging level at which this handler will be triggered
      * @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 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);
         parent::__construct($facility, $level, $bubble);
 
 
+        $this->ident = $ident;
+
         $this->socket = new UdpSocket($host, $port ?: 514);
         $this->socket = new UdpSocket($host, $port ?: 514);
     }
     }
 
 
@@ -69,7 +73,22 @@ class SyslogUdpHandler extends AbstractSyslogHandler
     {
     {
         $priority = $severity + $this->facility;
         $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);
     }
     }
 
 
     /**
     /**

+ 15 - 4
tests/Monolog/Handler/SyslogUdpHandlerTest.php

@@ -23,21 +23,32 @@ class SyslogUdpHandlerTest extends TestCase
      */
      */
     public function testWeValidateFacilities()
     public function testWeValidateFacilities()
     {
     {
-        $handler = new SyslogUdpHandler("ip", null, "invalidFacility");
+        $handler = new SyslogUdpHandler("ip", null, "php", "invalidFacility");
     }
     }
 
 
     public function testWeSplitIntoLines()
     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());
         $handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
 
 
         $socket = $this->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', array('write'), array('lol', 'lol'));
         $socket = $this->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', array('write'), array('lol', 'lol'));
         $socket->expects($this->at(0))
         $socket->expects($this->at(0))
             ->method('write')
             ->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))
         $socket->expects($this->at(1))
             ->method('write')
             ->method('write')
-            ->with("hej", "<".(LOG_AUTHPRIV + LOG_WARNING).">1 ");
+            ->with("hej", "<".(LOG_AUTHPRIV + LOG_WARNING).">1 $time $host php $pid - - ");
 
 
         $handler->setSocket($socket);
         $handler->setSocket($socket);