From 58d16ba358f9c4c3612947ad59b0ef9a364e4acd Mon Sep 17 00:00:00 2001 From: Craig D'Amelio Date: Wed, 24 Sep 2014 12:54:46 -0400 Subject: [PATCH 1/4] Group handler that ignores failures This allows a group handler which on a single handler failing will continue and allow subsequent handlers to record the event. --- .../Handler/WhatFailureGroupHandler.php | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/Monolog/Handler/WhatFailureGroupHandler.php diff --git a/src/Monolog/Handler/WhatFailureGroupHandler.php b/src/Monolog/Handler/WhatFailureGroupHandler.php new file mode 100644 index 00000000..e873f673 --- /dev/null +++ b/src/Monolog/Handler/WhatFailureGroupHandler.php @@ -0,0 +1,48 @@ + + */ +class WhatFailureGroupHandler extends GroupHandler +{ + /** + * {@inheritdoc} + */ + public function handle(array $record) + { + if ($this->processors) { + foreach ($this->processors as $processor) { + $record = call_user_func($processor, $record); + } + } + + foreach ($this->handlers as $handler) { + try { + $handler->handle($record); + } catch (\Exception $e) { + // What failure? + } + } + + return false === $this->bubble; + } + + /** + * {@inheritdoc} + */ + public function handleBatch(array $records) + { + foreach ($this->handlers as $handler) { + try { + $handler->handleBatch($records); + } catch (\Exception $e) { + // What failure? + } + } + } +} From 5b52bc05bab68ad18af5a26f9ebd9d80b080835f Mon Sep 17 00:00:00 2001 From: Craig D'Amelio Date: Wed, 24 Sep 2014 13:18:58 -0400 Subject: [PATCH 2/4] Update readme to indicate why you might use WhatFailureGroupHandler --- README.mdown | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.mdown b/README.mdown index 4af0075d..2fd9daf5 100644 --- a/README.mdown +++ b/README.mdown @@ -182,6 +182,10 @@ Handlers to the wrapped handler. - _TestHandler_: Used for testing, it records everything that is sent to it and has accessors to read out the information. +- _WhatFailureGroupHandler_: This handle extends the _GroupHandler_ ignoring + exceptions raised by each child handler. This allows you to ignore issues + where a remote tcp connection may have died but you do not want your entire + application to crash and may wish to continue to log to other handlers. Formatters ---------- From d7939f3463a2563a7684d35c27ec0f76df2af561 Mon Sep 17 00:00:00 2001 From: Craig D'Amelio Date: Wed, 24 Sep 2014 13:45:06 -0400 Subject: [PATCH 3/4] Test suite and supporting handler for WhatFailureGroupHandler --- src/Monolog/Handler/ExceptionTestHandler.php | 25 ++++ .../Handler/WhatFailureGroupHandlerTest.php | 107 ++++++++++++++++++ 2 files changed, 132 insertions(+) create mode 100644 src/Monolog/Handler/ExceptionTestHandler.php create mode 100644 tests/Monolog/Handler/WhatFailureGroupHandlerTest.php diff --git a/src/Monolog/Handler/ExceptionTestHandler.php b/src/Monolog/Handler/ExceptionTestHandler.php new file mode 100644 index 00000000..697e573b --- /dev/null +++ b/src/Monolog/Handler/ExceptionTestHandler.php @@ -0,0 +1,25 @@ + + */ +class ExceptionTestHandler extends TestHandler +{ + /** + * {@inheritdoc} + */ + public function handle(array $record) { + $return = parent::handle($record); + throw new \Exception("ExceptionTestHandler::handle"); + } +} diff --git a/tests/Monolog/Handler/WhatFailureGroupHandlerTest.php b/tests/Monolog/Handler/WhatFailureGroupHandlerTest.php new file mode 100644 index 00000000..29508afa --- /dev/null +++ b/tests/Monolog/Handler/WhatFailureGroupHandlerTest.php @@ -0,0 +1,107 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler; + +use Monolog\TestCase; +use Monolog\Logger; + +class WhatFailureGroupHandlerTest extends TestCase +{ + /** + * @covers Monolog\Handler\WhatFailureGroupHandler::__construct + * @expectedException InvalidArgumentException + */ + public function testConstructorOnlyTakesHandler() + { + new WhatFailureGroupHandler(array(new TestHandler(), "foo")); + } + + /** + * @covers Monolog\Handler\WhatFailureGroupHandler::__construct + * @covers Monolog\Handler\WhatFailureGroupHandler::handle + */ + public function testHandle() + { + $testHandlers = array(new TestHandler(), new TestHandler()); + $handler = new WhatFailureGroupHandler($testHandlers); + $handler->handle($this->getRecord(Logger::DEBUG)); + $handler->handle($this->getRecord(Logger::INFO)); + foreach ($testHandlers as $test) { + $this->assertTrue($test->hasDebugRecords()); + $this->assertTrue($test->hasInfoRecords()); + $this->assertTrue(count($test->getRecords()) === 2); + } + } + + /** + * @covers Monolog\Handler\WhatFailureGroupHandler::handleBatch + */ + public function testHandleBatch() + { + $testHandlers = array(new TestHandler(), new TestHandler()); + $handler = new WhatFailureGroupHandler($testHandlers); + $handler->handleBatch(array($this->getRecord(Logger::DEBUG), $this->getRecord(Logger::INFO))); + foreach ($testHandlers as $test) { + $this->assertTrue($test->hasDebugRecords()); + $this->assertTrue($test->hasInfoRecords()); + $this->assertTrue(count($test->getRecords()) === 2); + } + } + + /** + * @covers Monolog\Handler\WhatFailureGroupHandler::isHandling + */ + public function testIsHandling() + { + $testHandlers = array(new TestHandler(Logger::ERROR), new TestHandler(Logger::WARNING)); + $handler = new WhatFailureGroupHandler($testHandlers); + $this->assertTrue($handler->isHandling($this->getRecord(Logger::ERROR))); + $this->assertTrue($handler->isHandling($this->getRecord(Logger::WARNING))); + $this->assertFalse($handler->isHandling($this->getRecord(Logger::DEBUG))); + } + + /** + * @covers Monolog\Handler\WhatFailureGroupHandler::handle + */ + public function testHandleUsesProcessors() + { + $test = new TestHandler(); + $handler = new WhatFailureGroupHandler(array($test)); + $handler->pushProcessor(function ($record) { + $record['extra']['foo'] = true; + + return $record; + }); + $handler->handle($this->getRecord(Logger::WARNING)); + $this->assertTrue($test->hasWarningRecords()); + $records = $test->getRecords(); + $this->assertTrue($records[0]['extra']['foo']); + } + + /** + * @covers Monolog\Handler\WhatFailureGroupHandler::handle + */ + public function testHandleException() { + $test = new TestHandler(); + $exception = new ExceptionTestHandler(); + $handler = new WhatFailureGroupHandler(array($exception, $test, $exception)); + $handler->pushProcessor(function ($record) { + $record['extra']['foo'] = true; + + return $record; + }); + $handler->handle($this->getRecord(Logger::WARNING)); + $this->assertTrue($test->hasWarningRecords()); + $records = $test->getRecords(); + $this->assertTrue($records[0]['extra']['foo']); + } +} From 14207dfbf7e3b2609dd41eac627cb148d165c0a8 Mon Sep 17 00:00:00 2001 From: Craig D'Amelio Date: Wed, 24 Sep 2014 13:48:03 -0400 Subject: [PATCH 4/4] Strip documentation, don't want to overstate. --- tests/Monolog/Handler/WhatFailureGroupHandlerTest.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/Monolog/Handler/WhatFailureGroupHandlerTest.php b/tests/Monolog/Handler/WhatFailureGroupHandlerTest.php index 29508afa..f9695b44 100644 --- a/tests/Monolog/Handler/WhatFailureGroupHandlerTest.php +++ b/tests/Monolog/Handler/WhatFailureGroupHandlerTest.php @@ -1,14 +1,5 @@ - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - namespace Monolog\Handler; use Monolog\TestCase;