瀏覽代碼

Add possibility to use RFC3164 for udp syslog

Dominik Kukacka 6 年之前
父節點
當前提交
80e8b0d575
共有 2 個文件被更改,包括 59 次插入8 次删除
  1. 29 8
      src/Monolog/Handler/SyslogUdpHandler.php
  2. 30 0
      tests/Monolog/Handler/SyslogUdpHandlerTest.php

+ 29 - 8
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 <nulpunkt@gmail.com>
+ * @author Dominik Kukacka <dominik.kukacka@gmail.com>
  */
 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]);
     }
 
     /**

+ 30 - 0
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');