diff --git a/src/Monolog/Handler/StreamHandler.php b/src/Monolog/Handler/StreamHandler.php index ad6960cb..74a613cb 100644 --- a/src/Monolog/Handler/StreamHandler.php +++ b/src/Monolog/Handler/StreamHandler.php @@ -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);