Browse Source

Lazy-initialize the buffer handler, fixes #359

Jordi Boggiano 11 years ago
parent
commit
0d72125865
1 changed files with 7 additions and 3 deletions
  1. 7 3
      src/Monolog/Handler/BufferHandler.php

+ 7 - 3
src/Monolog/Handler/BufferHandler.php

@@ -28,6 +28,7 @@ class BufferHandler extends AbstractHandler
     protected $bufferLimit;
     protected $flushOnOverflow;
     protected $buffer = array();
+    protected $initialized = false;
 
     /**
      * @param HandlerInterface $handler         Handler.
@@ -42,9 +43,6 @@ class BufferHandler extends AbstractHandler
         $this->handler = $handler;
         $this->bufferLimit = (int) $bufferLimit;
         $this->flushOnOverflow = $flushOnOverflow;
-
-        // __destructor() doesn't get called on Fatal errors
-        register_shutdown_function(array($this, 'close'));
     }
 
     /**
@@ -56,6 +54,12 @@ class BufferHandler extends AbstractHandler
             return false;
         }
 
+        if (!$this->initialized) {
+            // __destructor() doesn't get called on Fatal errors
+            register_shutdown_function(array($this, 'close'));
+            $this->initialized = true;
+        }
+
         if ($this->bufferLimit > 0 && $this->bufferSize === $this->bufferLimit) {
             if ($this->flushOnOverflow) {
                 $this->flush();