Explorar el Código

Merge pull request #373 from DavidMikeSimon/fingers_crossed_passthru_level

Allowing some messages to always be flushed from FingersCrossedHandler
Jordi Boggiano hace 11 años
padre
commit
fd99185038

+ 21 - 1
src/Monolog/Handler/FingersCrossedHandler.php

@@ -35,6 +35,7 @@ class FingersCrossedHandler extends AbstractHandler
     protected $bufferSize;
     protected $buffer = array();
     protected $stopBuffering;
+    protected $passthruLevel;
 
     /**
      * @param callable|HandlerInterface       $handler            Handler or factory callable($record, $fingersCrossedHandler).
@@ -42,8 +43,9 @@ class FingersCrossedHandler extends AbstractHandler
      * @param int                             $bufferSize         How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
      * @param Boolean                         $bubble             Whether the messages that are handled can bubble up the stack or not
      * @param Boolean                         $stopBuffering      Whether the handler should stop buffering after being triggered (default true)
+     * @param int                             $passthruLevel      Minimum level to always flush to handler on close, even if strategy not triggered
      */
-    public function __construct($handler, $activationStrategy = null, $bufferSize = 0, $bubble = true, $stopBuffering = true)
+    public function __construct($handler, $activationStrategy = null, $bufferSize = 0, $bubble = true, $stopBuffering = true, $passthruLevel = NULL)
     {
         if (null === $activationStrategy) {
             $activationStrategy = new ErrorLevelActivationStrategy(Logger::WARNING);
@@ -59,6 +61,7 @@ class FingersCrossedHandler extends AbstractHandler
         $this->bufferSize = $bufferSize;
         $this->bubble = $bubble;
         $this->stopBuffering = $stopBuffering;
+        $this->passthruLevel = $passthruLevel;
     }
 
     /**
@@ -108,6 +111,23 @@ class FingersCrossedHandler extends AbstractHandler
         return false === $this->bubble;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function close()
+    {
+        if (NULL !== $this->passthruLevel) {
+            $level = $this->passthruLevel;
+            $this->buffer = array_filter($this->buffer, function ($record) use ($level) {
+                return $record['level'] >= $level;
+            });
+            if (count($this->buffer) > 0) {
+                $this->handler->handleBatch($this->buffer);
+                $this->buffer = array();
+            }
+        }
+    }
+
     /**
      * Resets the state of the handler. Stops forwarding records to the wrapped handler.
      */

+ 18 - 0
tests/Monolog/Handler/FingersCrossedHandlerTest.php

@@ -31,6 +31,7 @@ class FingersCrossedHandlerTest extends TestCase
         $this->assertFalse($test->hasDebugRecords());
         $this->assertFalse($test->hasInfoRecords());
         $handler->handle($this->getRecord(Logger::WARNING));
+        $handler->close();
         $this->assertTrue($test->hasInfoRecords());
         $this->assertTrue(count($test->getRecords()) === 3);
     }
@@ -44,6 +45,7 @@ class FingersCrossedHandlerTest extends TestCase
         $handler = new FingersCrossedHandler($test);
         $handler->handle($this->getRecord(Logger::WARNING));
         $handler->handle($this->getRecord(Logger::DEBUG));
+        $handler->close();
         $this->assertTrue($test->hasWarningRecords());
         $this->assertTrue($test->hasDebugRecords());
     }
@@ -60,6 +62,7 @@ class FingersCrossedHandlerTest extends TestCase
         $handler->handle($this->getRecord(Logger::DEBUG));
         $handler->reset();
         $handler->handle($this->getRecord(Logger::INFO));
+        $handler->close();
         $this->assertTrue($test->hasWarningRecords());
         $this->assertTrue($test->hasDebugRecords());
         $this->assertFalse($test->hasInfoRecords());
@@ -75,6 +78,7 @@ class FingersCrossedHandlerTest extends TestCase
         $handler->handle($this->getRecord(Logger::DEBUG));
         $handler->handle($this->getRecord(Logger::WARNING));
         $handler->handle($this->getRecord(Logger::INFO));
+        $handler->close();
         $this->assertTrue($test->hasWarningRecords());
         $this->assertTrue($test->hasDebugRecords());
         $this->assertFalse($test->hasInfoRecords());
@@ -186,4 +190,18 @@ class FingersCrossedHandlerTest extends TestCase
         $records = $test->getRecords();
         $this->assertTrue($records[0]['extra']['foo']);
     }
+
+    /**
+     * @covers Monolog\Handler\FingersCrossedHandler::close
+     */
+    public function testPassthruOnClose()
+    {
+        $test = new TestHandler();
+        $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy(Logger::WARNING), 0, true, true, Logger::INFO);
+        $handler->handle($this->getRecord(Logger::DEBUG));
+        $handler->handle($this->getRecord(Logger::INFO));
+        $handler->close();
+        $this->assertFalse($test->hasDebugRecords());
+        $this->assertTrue($test->hasInfoRecords());
+    }
 }