From c677398c76961bcbe55ec1da2cedce5585df6577 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 14 Apr 2016 16:56:59 -0400 Subject: [PATCH 1/5] Provide means to manually activate a FingersCrossedHandler This is useful if you have a FingersCrossedHandler set up and some where in your application are interested in all output regardless of whether the threshold is actually reached. For example to temporarily debug some problem. One can replace the handler in that spot, but this is signifcantly easier to use. --- src/Monolog/Handler/FingersCrossedHandler.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Monolog/Handler/FingersCrossedHandler.php b/src/Monolog/Handler/FingersCrossedHandler.php index 30a85dd6..6036c15e 100644 --- a/src/Monolog/Handler/FingersCrossedHandler.php +++ b/src/Monolog/Handler/FingersCrossedHandler.php @@ -36,6 +36,7 @@ class FingersCrossedHandler extends AbstractHandler protected $buffer = array(); protected $stopBuffering; protected $passthruLevel; + protected $overrideActivated = false; /** * @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler). @@ -79,6 +80,14 @@ class FingersCrossedHandler extends AbstractHandler return true; } + /** + * Manually activate this logger regardless of the activation strategy + */ + public function activate() + { + $this->overrideActivated = true; + } + /** * {@inheritdoc} */ @@ -95,7 +104,7 @@ class FingersCrossedHandler extends AbstractHandler if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) { array_shift($this->buffer); } - if ($this->activationStrategy->isHandlerActivated($record)) { + if ($this->overrideActivated || $this->activationStrategy->isHandlerActivated($record)) { if ($this->stopBuffering) { $this->buffering = false; } From e0b521ba53b375421c41e63544b5e85d0a670f09 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 14 Apr 2016 17:02:29 -0400 Subject: [PATCH 2/5] Add a test that verifies manual overriding of activation strategy works --- .../Handler/FingersCrossedHandlerTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Monolog/Handler/FingersCrossedHandlerTest.php b/tests/Monolog/Handler/FingersCrossedHandlerTest.php index 8e31e9b8..f87c632e 100644 --- a/tests/Monolog/Handler/FingersCrossedHandlerTest.php +++ b/tests/Monolog/Handler/FingersCrossedHandlerTest.php @@ -173,6 +173,22 @@ class FingersCrossedHandlerTest extends TestCase $this->assertTrue($test->hasWarningRecords()); } + /** + * @covers Monolog\Handler\FingersCrossedHandler::__construct + * @covers Monolog\Handler\FingersCrossedHandler::activate + */ + public function testOverrideActivationStrategy() + { + $test = new TestHandler(); + $handler = new FingersCrossedHandler($test, new ErrorLevelActivationStrategy('warning')); + $handler->handle($this->getRecord(Logger::DEBUG)); + $this->assertFalse($test->hasDebugRecords()); + $handler->activate(); + $handler->handle($this->getRecord(Logger::INFO)); + $this->assertTrue($test->hasDebugRecords()); + $this->assertTrue($test->hasInfoRecords()); + } + /** * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::__construct * @covers Monolog\Handler\FingersCrossed\ChannelLevelActivationStrategy::isHandlerActivated From abb58081fe729573c830107a99f0331a3eda4005 Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Fri, 15 Apr 2016 14:05:55 -0400 Subject: [PATCH 3/5] Extract fingerscrossed activation into its own method callable from outside --- src/Monolog/Handler/FingersCrossedHandler.php | 32 +++++++++++-------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Monolog/Handler/FingersCrossedHandler.php b/src/Monolog/Handler/FingersCrossedHandler.php index 6036c15e..8a3b69ca 100644 --- a/src/Monolog/Handler/FingersCrossedHandler.php +++ b/src/Monolog/Handler/FingersCrossedHandler.php @@ -36,7 +36,6 @@ class FingersCrossedHandler extends AbstractHandler protected $buffer = array(); protected $stopBuffering; protected $passthruLevel; - protected $overrideActivated = false; /** * @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler). @@ -85,7 +84,22 @@ class FingersCrossedHandler extends AbstractHandler */ public function activate() { - $this->overrideActivated = true; + if ($this->stopBuffering) { + $this->buffering = false; + } + if (!$this->handler instanceof HandlerInterface) { + $record = end($this->buffer); + if ($record === false) { + $record = null; + } + + $this->handler = call_user_func($this->handler, $record, $this); + if (!$this->handler instanceof HandlerInterface) { + throw new \RuntimeException("The factory callable should return a HandlerInterface"); + } + } + $this->handler->handleBatch($this->buffer); + $this->buffer = array(); } /** @@ -104,18 +118,8 @@ class FingersCrossedHandler extends AbstractHandler if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) { array_shift($this->buffer); } - if ($this->overrideActivated || $this->activationStrategy->isHandlerActivated($record)) { - if ($this->stopBuffering) { - $this->buffering = false; - } - if (!$this->handler instanceof HandlerInterface) { - $this->handler = call_user_func($this->handler, $record, $this); - if (!$this->handler instanceof HandlerInterface) { - throw new \RuntimeException("The factory callable should return a HandlerInterface"); - } - } - $this->handler->handleBatch($this->buffer); - $this->buffer = array(); + if ($this->activationStrategy->isHandlerActivated($record)) { + $this->activate($record); } } else { $this->handler->handle($record); From e7c326782f72938741a96c3157af7fd186d170bc Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Fri, 15 Apr 2016 14:11:04 -0400 Subject: [PATCH 4/5] FingersCrossed: Adapt test for extraction of activate from handle() --- tests/Monolog/Handler/FingersCrossedHandlerTest.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/Monolog/Handler/FingersCrossedHandlerTest.php b/tests/Monolog/Handler/FingersCrossedHandlerTest.php index f87c632e..b92bf437 100644 --- a/tests/Monolog/Handler/FingersCrossedHandlerTest.php +++ b/tests/Monolog/Handler/FingersCrossedHandlerTest.php @@ -22,6 +22,7 @@ class FingersCrossedHandlerTest extends TestCase /** * @covers Monolog\Handler\FingersCrossedHandler::__construct * @covers Monolog\Handler\FingersCrossedHandler::handle + * @covers Monolog\Handler\FingersCrossedHandler::activate */ public function testHandleBuffers() { @@ -39,6 +40,7 @@ class FingersCrossedHandlerTest extends TestCase /** * @covers Monolog\Handler\FingersCrossedHandler::handle + * @covers Monolog\Handler\FingersCrossedHandler::activate */ public function testHandleStopsBufferingAfterTrigger() { @@ -53,6 +55,7 @@ class FingersCrossedHandlerTest extends TestCase /** * @covers Monolog\Handler\FingersCrossedHandler::handle + * @covers Monolog\Handler\FingersCrossedHandler::activate * @covers Monolog\Handler\FingersCrossedHandler::reset */ public function testHandleRestartBufferingAfterReset() @@ -71,6 +74,7 @@ class FingersCrossedHandlerTest extends TestCase /** * @covers Monolog\Handler\FingersCrossedHandler::handle + * @covers Monolog\Handler\FingersCrossedHandler::activate */ public function testHandleRestartBufferingAfterBeingTriggeredWhenStopBufferingIsDisabled() { @@ -87,6 +91,7 @@ class FingersCrossedHandlerTest extends TestCase /** * @covers Monolog\Handler\FingersCrossedHandler::handle + * @covers Monolog\Handler\FingersCrossedHandler::activate */ public function testHandleBufferLimit() { @@ -103,6 +108,7 @@ class FingersCrossedHandlerTest extends TestCase /** * @covers Monolog\Handler\FingersCrossedHandler::handle + * @covers Monolog\Handler\FingersCrossedHandler::activate */ public function testHandleWithCallback() { @@ -121,6 +127,7 @@ class FingersCrossedHandlerTest extends TestCase /** * @covers Monolog\Handler\FingersCrossedHandler::handle + * @covers Monolog\Handler\FingersCrossedHandler::activate * @expectedException RuntimeException */ public function testHandleWithBadCallbackThrowsException() @@ -184,8 +191,8 @@ class FingersCrossedHandlerTest extends TestCase $handler->handle($this->getRecord(Logger::DEBUG)); $this->assertFalse($test->hasDebugRecords()); $handler->activate(); - $handler->handle($this->getRecord(Logger::INFO)); $this->assertTrue($test->hasDebugRecords()); + $handler->handle($this->getRecord(Logger::INFO)); $this->assertTrue($test->hasInfoRecords()); } @@ -225,6 +232,7 @@ class FingersCrossedHandlerTest extends TestCase /** * @covers Monolog\Handler\FingersCrossedHandler::handle + * @covers Monolog\Handler\FingersCrossedHandler::activate */ public function testHandleUsesProcessors() { From 0fc6734c6f2c133a73e073472f33ecd87247894d Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Wed, 20 Apr 2016 15:00:33 -0400 Subject: [PATCH 5/5] FingersCrossedHandler: Clean up code --- src/Monolog/Handler/FingersCrossedHandler.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/Monolog/Handler/FingersCrossedHandler.php b/src/Monolog/Handler/FingersCrossedHandler.php index 8a3b69ca..d1dcaacf 100644 --- a/src/Monolog/Handler/FingersCrossedHandler.php +++ b/src/Monolog/Handler/FingersCrossedHandler.php @@ -88,10 +88,7 @@ class FingersCrossedHandler extends AbstractHandler $this->buffering = false; } if (!$this->handler instanceof HandlerInterface) { - $record = end($this->buffer); - if ($record === false) { - $record = null; - } + $record = end($this->buffer) ?: null; $this->handler = call_user_func($this->handler, $record, $this); if (!$this->handler instanceof HandlerInterface) { @@ -119,7 +116,7 @@ class FingersCrossedHandler extends AbstractHandler array_shift($this->buffer); } if ($this->activationStrategy->isHandlerActivated($record)) { - $this->activate($record); + $this->activate(); } } else { $this->handler->handle($record);