Przeglądaj źródła

added possibility to set child handler after instantiation (#1946)

Andrii Shevchenko 10 miesięcy temu
rodzic
commit
c8bbe52af5

+ 5 - 0
src/Monolog/Handler/BufferHandler.php

@@ -162,4 +162,9 @@ class BufferHandler extends AbstractHandler implements ProcessableHandlerInterfa
 
 
         throw new \UnexpectedValueException('The nested handler of type '.\get_class($this->handler).' does not support formatters.');
         throw new \UnexpectedValueException('The nested handler of type '.\get_class($this->handler).' does not support formatters.');
     }
     }
+
+    public function setHandler(HandlerInterface $handler)
+    {
+        $this->handler = $handler;
+    }
 }
 }

+ 20 - 0
tests/Monolog/Handler/BufferHandlerTest.php

@@ -155,4 +155,24 @@ class BufferHandlerTest extends TestCase
         $records = $test->getRecords();
         $records = $test->getRecords();
         $this->assertTrue($records[0]['extra']['foo']);
         $this->assertTrue($records[0]['extra']['foo']);
     }
     }
+
+    public function testSetHandler()
+    {
+        $testOriginal = new TestHandler();
+        $handler = new BufferHandler($testOriginal);
+        $handler->handle($this->getRecord(Level::Info));
+
+        $testNew = new TestHandler();
+        $handler->setHandler($testNew);
+
+        $handler->handle($this->getRecord(Level::Debug));
+
+        $handler->close();
+
+        $this->assertFalse($testOriginal->hasInfoRecords());
+        $this->assertFalse($testOriginal->hasDebugRecords());
+        $this->assertTrue($testNew->hasInfoRecords());
+        $this->assertTrue($testNew->hasDebugRecords());
+        $this->assertCount(2, $testNew->getRecords());
+    }
 }
 }