1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-05 12:47:39 +02:00

Set StreamHandler stream chunk size, backport #1552

This ensures atomic log record writing to the output stream.
This commit is contained in:
David Schneider
2021-05-07 11:35:18 +02:00
committed by Jordi Boggiano
parent ea9d86160b
commit 07a0d1ed33

View File

@@ -23,6 +23,10 @@ use Monolog\Utils;
*/
class StreamHandler extends AbstractProcessingHandler
{
/** @private 512KB */
const CHUNK_SIZE = 524288;
/** @var resource|null */
protected $stream;
protected $url;
private $errorMessage;
@@ -45,6 +49,7 @@ class StreamHandler extends AbstractProcessingHandler
parent::__construct($level, $bubble);
if (is_resource($stream)) {
$this->stream = $stream;
$this->streamSetChunkSize();
} elseif (is_string($stream)) {
$this->url = Utils::canonicalizePath($stream);
} else {
@@ -109,6 +114,7 @@ class StreamHandler extends AbstractProcessingHandler
throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened in append mode: '.$this->errorMessage, $this->url));
}
$this->streamSetChunkSize();
}
if ($this->useLocking) {
@@ -133,6 +139,15 @@ class StreamHandler extends AbstractProcessingHandler
fwrite($stream, (string) $record['formatted']);
}
protected function streamSetChunkSize()
{
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
return stream_set_chunk_size($this->stream, self::CHUNK_SIZE);
}
return false;
}
private function customErrorHandler($code, $msg)
{
$this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);