1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-08 06:06:40 +02:00

Make sure handlers can be closed multiple times

This commit is contained in:
Jordi Boggiano
2015-08-09 17:29:31 +01:00
parent d93492fb60
commit 3dccef613e
4 changed files with 40 additions and 2 deletions

View File

@@ -29,6 +29,13 @@ class RollbarHandler extends AbstractProcessingHandler
*/ */
protected $rollbarNotifier; protected $rollbarNotifier;
/**
* Records whether any log records have been added since the last flush of the rollbar notifier
*
* @var bool
*/
private $hasRecords = false;
/** /**
* @param RollbarNotifier $rollbarNotifier RollbarNotifier object constructed with valid token * @param RollbarNotifier $rollbarNotifier RollbarNotifier object constructed with valid token
* @param integer $level The minimum logging level at which this handler will be triggered * @param integer $level The minimum logging level at which this handler will be triggered
@@ -61,6 +68,8 @@ class RollbarHandler extends AbstractProcessingHandler
array_merge($record['context'], $record['extra'], $extraData) array_merge($record['context'], $record['extra'], $extraData)
); );
} }
$this->hasRecords = true;
} }
/** /**
@@ -68,6 +77,9 @@ class RollbarHandler extends AbstractProcessingHandler
*/ */
public function close() public function close()
{ {
if ($this->hasRecords) {
$this->rollbarNotifier->flush(); $this->rollbarNotifier->flush();
$this->hasRecords = false;
}
} }
} }

View File

@@ -118,6 +118,8 @@ class RotatingFileHandler extends StreamHandler
unlink($file); unlink($file);
} }
} }
$this->mustRotate = false;
} }
protected function getTimedFilename() protected function getTimedFilename()

View File

@@ -29,11 +29,17 @@ class UdpSocket
public function close() public function close()
{ {
if (is_resource($this->socket)) {
socket_close($this->socket); socket_close($this->socket);
$this->socket = null;
}
} }
protected function send($chunk) protected function send($chunk)
{ {
if (!is_resource($this->socket)) {
throw new \LogicException('The UdpSocket to '.$this->ip.':'.$this->port.' has been closed and can not be written to anymore');
}
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);
} }

View File

@@ -12,6 +12,7 @@
namespace Monolog\Handler; namespace Monolog\Handler;
use Monolog\TestCase; use Monolog\TestCase;
use Monolog\Handler\SyslogUdp\UdpSocket;
/** /**
* @requires extension sockets * @requires extension sockets
@@ -43,4 +44,21 @@ class UdpSocketTest extends TestCase
$socket->write($longString, "HEADER"); $socket->write($longString, "HEADER");
} }
public function testDoubleCloseDoesNotError()
{
$socket = new UdpSocket('127.0.0.1', 514);
$socket->close();
$socket->close();
}
/**
* @expectedException LogicException
*/
public function testWriteAfterCloseErrors()
{
$socket = new UdpSocket('127.0.0.1', 514);
$socket->close();
$socket->write('foo', "HEADER");
}
} }