Explorar o código

Set StreamHandler stream chunk size, backport #1552

This ensures atomic log record writing to the output stream.
David Schneider %!s(int64=4) %!d(string=hai) anos
pai
achega
07a0d1ed33
Modificáronse 1 ficheiros con 15 adicións e 0 borrados
  1. 15 0
      src/Monolog/Handler/StreamHandler.php

+ 15 - 0
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);