mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-03 11:47:38 +02:00
Fix syslog transport to follow the spec closer, fixes #476
This commit is contained in:
@@ -13,7 +13,7 @@ namespace Monolog\Handler\SyslogUdp;
|
|||||||
|
|
||||||
class UdpSocket
|
class UdpSocket
|
||||||
{
|
{
|
||||||
const DATAGRAM_MAX_LENGTH = 2048;
|
const DATAGRAM_MAX_LENGTH = 65023;
|
||||||
|
|
||||||
public function __construct($ip, $port = 514)
|
public function __construct($ip, $port = 514)
|
||||||
{
|
{
|
||||||
@@ -24,11 +24,7 @@ class UdpSocket
|
|||||||
|
|
||||||
public function write($line, $header = "")
|
public function write($line, $header = "")
|
||||||
{
|
{
|
||||||
$remaining = $line;
|
$this->send($this->assembleMessage($line, $header));
|
||||||
while (!is_null($remaining)) {
|
|
||||||
list($chunk, $remaining) = $this->splitLineIfNessesary($remaining, $header);
|
|
||||||
$this->send($chunk);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function close()
|
public function close()
|
||||||
@@ -41,22 +37,10 @@ class UdpSocket
|
|||||||
socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port);
|
socket_sendto($this->socket, $chunk, strlen($chunk), $flags = 0, $this->ip, $this->port);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function splitLineIfNessesary($line, $header)
|
protected function assembleMessage($line, $header)
|
||||||
{
|
{
|
||||||
if ($this->shouldSplitLine($line, $header)) {
|
|
||||||
$chunkSize = self::DATAGRAM_MAX_LENGTH - strlen($header);
|
$chunkSize = self::DATAGRAM_MAX_LENGTH - strlen($header);
|
||||||
$chunk = $header . substr($line, 0, $chunkSize);
|
|
||||||
$remaining = substr($line, $chunkSize);
|
|
||||||
} else {
|
|
||||||
$chunk = $header . $line;
|
|
||||||
$remaining = null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return array($chunk, $remaining);
|
return $header . substr($line, 0, $chunkSize);
|
||||||
}
|
|
||||||
|
|
||||||
protected function shouldSplitLine($remaining, $header)
|
|
||||||
{
|
|
||||||
return strlen($header.$remaining) > self::DATAGRAM_MAX_LENGTH;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -18,7 +18,7 @@ use Monolog\TestCase;
|
|||||||
*/
|
*/
|
||||||
class UdpSocketTest extends TestCase
|
class UdpSocketTest extends TestCase
|
||||||
{
|
{
|
||||||
public function testWeDoNotSplitShortMessages()
|
public function testWeDoNotTruncateShortMessages()
|
||||||
{
|
{
|
||||||
$socket = $this->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', array('send'), array('lol', 'lol'));
|
$socket = $this->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', array('send'), array('lol', 'lol'));
|
||||||
|
|
||||||
@@ -29,29 +29,18 @@ class UdpSocketTest extends TestCase
|
|||||||
$socket->write("The quick brown fox jumps over the lazy dog", "HEADER: ");
|
$socket->write("The quick brown fox jumps over the lazy dog", "HEADER: ");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testWeSplitLongMessages()
|
public function testLongMessagesAreTruncated()
|
||||||
{
|
{
|
||||||
$socket = $this->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', array('send'), array('lol', 'lol'));
|
$socket = $this->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', array('send'), array('lol', 'lol'));
|
||||||
|
|
||||||
$socket->expects($this->at(1))
|
$truncatedString = str_repeat("derp", 16254).'d';
|
||||||
|
|
||||||
|
$socket->expects($this->exactly(1))
|
||||||
->method('send')
|
->method('send')
|
||||||
->with("The quick brown fox jumps over the lazy dog");
|
->with("HEADER" . $truncatedString);
|
||||||
|
|
||||||
$aStringOfLength2048 = str_repeat("derp", 2048/4);
|
$longString = str_repeat("derp", 20000);
|
||||||
|
|
||||||
$socket->write($aStringOfLength2048."The quick brown fox jumps over the lazy dog");
|
$socket->write($longString, "HEADER");
|
||||||
}
|
|
||||||
|
|
||||||
public function testAllSplitMessagesHasAHeader()
|
|
||||||
{
|
|
||||||
$socket = $this->getMock('\Monolog\Handler\SyslogUdp\UdpSocket', array('send'), array('lol', 'lol'));
|
|
||||||
|
|
||||||
$socket->expects($this->exactly(5))
|
|
||||||
->method('send')
|
|
||||||
->with($this->stringStartsWith("HEADER"));
|
|
||||||
|
|
||||||
$aStringOfLength8192 = str_repeat("derp", 2048);
|
|
||||||
|
|
||||||
$socket->write($aStringOfLength8192, "HEADER");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user