mirror of
https://github.com/Seldaek/monolog.git
synced 2025-08-04 12:17:35 +02:00
Make handlers more serializable by default by having them close() before sleeping, fixes #365
This commit is contained in:
@@ -43,4 +43,11 @@ abstract class Handler implements HandlerInterface
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
public function __sleep()
|
||||
{
|
||||
$this->close();
|
||||
|
||||
return array_keys(get_object_vars($this));
|
||||
}
|
||||
}
|
||||
|
@@ -51,13 +51,38 @@ class StreamHandlerTest extends TestCase
|
||||
{
|
||||
$handler = new StreamHandler('php://memory');
|
||||
$handler->handle($this->getRecord(Logger::WARNING, 'test'));
|
||||
$streamProp = new \ReflectionProperty('Monolog\Handler\StreamHandler', 'stream');
|
||||
$streamProp->setAccessible(true);
|
||||
$handle = $streamProp->getValue($handler);
|
||||
$stream = $handler->getStream();
|
||||
|
||||
$this->assertTrue(is_resource($handle));
|
||||
$this->assertTrue(is_resource($stream));
|
||||
$handler->close();
|
||||
$this->assertFalse(is_resource($handle));
|
||||
$this->assertFalse(is_resource($stream));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers Monolog\Handler\StreamHandler::close
|
||||
* @covers Monolog\Handler\Handler::__sleep
|
||||
*/
|
||||
public function testSerialization()
|
||||
{
|
||||
$handler = new StreamHandler('php://memory');
|
||||
$handler->handle($this->getRecord(Logger::WARNING, 'testfoo'));
|
||||
$stream = $handler->getStream();
|
||||
|
||||
$this->assertTrue(is_resource($stream));
|
||||
fseek($stream, 0);
|
||||
$this->assertContains('testfoo', stream_get_contents($stream));
|
||||
$serialized = serialize($handler);
|
||||
$this->assertFalse(is_resource($stream));
|
||||
|
||||
$handler = unserialize($serialized);
|
||||
$handler->handle($this->getRecord(Logger::WARNING, 'testbar'));
|
||||
$stream = $handler->getStream();
|
||||
|
||||
$this->assertTrue(is_resource($stream));
|
||||
fseek($stream, 0);
|
||||
$contents = stream_get_contents($stream);
|
||||
$this->assertNotContains('testfoo', $contents);
|
||||
$this->assertContains('testbar', $contents);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user