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

support for log file locking

This commit is contained in:
Ian Babrou
2014-06-30 14:27:26 +04:00
parent f84030a009
commit 6c799b3976
2 changed files with 25 additions and 1 deletions

View File

@@ -26,14 +26,16 @@ class StreamHandler extends AbstractProcessingHandler
protected $url; protected $url;
private $errorMessage; private $errorMessage;
protected $filePermission; protected $filePermission;
protected $preferLocking;
/** /**
* @param string $stream * @param string $stream
* @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
* @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 int $filePermissions Optional file permissions (default (0644) are only for owner read/write) * @param int $filePermissions Optional file permissions (default (0644) are only for owner read/write)
* @param Boolean $preferLocking Try to lock log file before doing any writes
*/ */
public function __construct($stream, $level = Logger::DEBUG, $bubble = true, $filePermission = null) public function __construct($stream, $level = Logger::DEBUG, $bubble = true, $filePermission = null, $preferLocking = false)
{ {
parent::__construct($level, $bubble); parent::__construct($level, $bubble);
if (is_resource($stream)) { if (is_resource($stream)) {
@@ -43,6 +45,7 @@ class StreamHandler extends AbstractProcessingHandler
} }
$this->filePermission = $filePermission; $this->filePermission = $filePermission;
$this->preferLocking = $preferLocking;
} }
/** /**
@@ -77,7 +80,17 @@ class StreamHandler extends AbstractProcessingHandler
throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$this->errorMessage, $this->url)); throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened: '.$this->errorMessage, $this->url));
} }
} }
if ($this->preferLocking) {
// ignoring errors here, there's not much we can do about them
flock($this->stream, LOCK_EX);
}
fwrite($this->stream, (string) $record['formatted']); fwrite($this->stream, (string) $record['formatted']);
if ($this->preferLocking) {
flock($this->stream, LOCK_UN);
}
} }
private function customErrorHandler($code, $msg) private function customErrorHandler($code, $msg)

View File

@@ -53,6 +53,17 @@ class StreamHandlerTest extends TestCase
$handler->handle($this->getRecord()); $handler->handle($this->getRecord());
} }
/**
* @covers Monolog\Handler\StreamHandler::__construct
* @covers Monolog\Handler\StreamHandler::write
*/
public function testWriteLocking()
{
$temp = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'monolog_locked_log';
$handler = new StreamHandler($temp, Logger::DEBUG, true, null, true);
$handler->handle($this->getRecord());
}
/** /**
* @expectedException LogicException * @expectedException LogicException
* @covers Monolog\Handler\StreamHandler::__construct * @covers Monolog\Handler\StreamHandler::__construct