Przeglądaj źródła

Merge branch '1.x'

Jordi Boggiano 8 lat temu
rodzic
commit
772a470c1a

+ 11 - 0
src/Monolog/Handler/SlackHandler.php

@@ -144,6 +144,17 @@ class SlackHandler extends SocketHandler
     protected function write(array $record)
     protected function write(array $record)
     {
     {
         parent::write($record);
         parent::write($record);
+        $this->finalizeWrite();
+    }
+
+    /**
+     * Finalizes the request by reading some bytes and then closing the socket
+     *
+     * If we do not read some but close the socket too early, slack sometimes
+     * drops the request entirely.
+     */
+    protected function finalizeWrite()
+    {
         $res = $this->getResource();
         $res = $this->getResource();
         if (is_resource($res)) {
         if (is_resource($res)) {
             @fread($res, 2048);
             @fread($res, 2048);

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

@@ -22,6 +22,7 @@ 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
@@ -29,11 +30,14 @@ class SyslogUdpHandler extends AbstractSyslogHandler
      * @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
+     * @param string  $ident    Program name or tag for each log message.
      */
      */
-    public function __construct($host, $port = 514, $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);
         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,24 @@ 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);
     }
     }
 
 
     /**
     /**

+ 14 - 3
tests/Monolog/Handler/SyslogUdpHandlerTest.php

@@ -28,7 +28,18 @@ class SyslogUdpHandlerTest extends TestCase
 
 
     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(array("127.0.0.1", 514, "authpriv"))
+            ->setMethods(array('getDateTime'))
+            ->getMock();
+
+        $handler->method('getDateTime')
+            ->willReturn($time);
+
         $handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
         $handler->setFormatter(new \Monolog\Formatter\ChromePHPFormatter());
 
 
         $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
         $socket = $this->getMockBuilder('Monolog\Handler\SyslogUdp\UdpSocket')
@@ -37,10 +48,10 @@ class SyslogUdpHandlerTest extends TestCase
             ->getMock();
             ->getMock();
         $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);