From 2b6437bd87b0f18b01ea6c8d5653e899a9cd89b0 Mon Sep 17 00:00:00 2001 From: lenar Date: Tue, 10 May 2011 15:52:43 +0300 Subject: [PATCH 1/6] Added ForwardHandler --- src/Monolog/Handler/ForwardHandler.php | 82 ++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/Monolog/Handler/ForwardHandler.php diff --git a/src/Monolog/Handler/ForwardHandler.php b/src/Monolog/Handler/ForwardHandler.php new file mode 100644 index 00000000..db0625a2 --- /dev/null +++ b/src/Monolog/Handler/ForwardHandler.php @@ -0,0 +1,82 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Monolog\Handler; + +use Monolog\Logger; + +/** + * Forwards records to multiple handlers + * + * @author Lenar Lõhmus + */ +class ForwardHandler extends AbstractHandler +{ + private $handlersInitialized; + protected $handlers; + + /** + * @param Array $handlers Array of Handlers or factory callbacks($record, $fingersCrossedHandler). + * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not + */ + public function __construct(array $handlers, $bubble = false) + { + $this->handlersInitialized = false; + $this->handlers = $handlers; + $this->bubble = $bubble; + } + + /** + * {@inheritdoc} + */ + public function handle(array $record) + { + $this->handlersInitialized || $this->initializeHandlers(); + + foreach ($this->handlers as $handler) { + $handler->handle($record); + } + return false === $this->bubble; + } + + /** + * {@inheritdoc} + */ + public function handleBatch(array $records) + { + $this->handlersInitialized || $this->initializeHandlers(); + + foreach ($this->handlers as $handler) { + $handler->handleBatch($records); + } + } + + /** + * Implemented to comply with the AbstractHandler requirements. Can not be called. + */ + protected function write(array $record) + { + throw new \BadMethodCallException('This method should not be called directly on the FingersCrossedHandler.'); + } + + private function initializeHandlers() + { + foreach ($this->handlers as &$handler) { + if (!$handler instanceof HandlerInterface) { + $handler = call_user_func($this->handler, $record, $this); + if (!$handler instanceof HandlerInterface) { + throw new \RuntimeException("The factory callback should return a HandlerInterface"); + } + } + } + $this->handlersInitialized = true; + } +} From 8d68d4cef4567a753e01df0bd2454dac2c73808b Mon Sep 17 00:00:00 2001 From: lenar Date: Tue, 10 May 2011 16:45:06 +0300 Subject: [PATCH 2/6] Fix variable name --- src/Monolog/Handler/ForwardHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Monolog/Handler/ForwardHandler.php b/src/Monolog/Handler/ForwardHandler.php index db0625a2..3a36dafe 100644 --- a/src/Monolog/Handler/ForwardHandler.php +++ b/src/Monolog/Handler/ForwardHandler.php @@ -71,7 +71,7 @@ class ForwardHandler extends AbstractHandler { foreach ($this->handlers as &$handler) { if (!$handler instanceof HandlerInterface) { - $handler = call_user_func($this->handler, $record, $this); + $handler = call_user_func($handler, $record, $this); if (!$handler instanceof HandlerInterface) { throw new \RuntimeException("The factory callback should return a HandlerInterface"); } From 6fcc54d09f1baadbdba0db20992c52b6f7c598fd Mon Sep 17 00:00:00 2001 From: lenar Date: Tue, 10 May 2011 16:51:09 +0300 Subject: [PATCH 3/6] Change the name to ForwarderHandler --- .../Handler/{ForwardHandler.php => ForwarderHandler.php} | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) rename src/Monolog/Handler/{ForwardHandler.php => ForwarderHandler.php} (95%) diff --git a/src/Monolog/Handler/ForwardHandler.php b/src/Monolog/Handler/ForwarderHandler.php similarity index 95% rename from src/Monolog/Handler/ForwardHandler.php rename to src/Monolog/Handler/ForwarderHandler.php index 3a36dafe..d34a1d00 100644 --- a/src/Monolog/Handler/ForwardHandler.php +++ b/src/Monolog/Handler/ForwarderHandler.php @@ -18,7 +18,7 @@ use Monolog\Logger; * * @author Lenar Lõhmus */ -class ForwardHandler extends AbstractHandler +class ForwarderHandler extends AbstractHandler { private $handlersInitialized; protected $handlers; @@ -64,7 +64,7 @@ class ForwardHandler extends AbstractHandler */ protected function write(array $record) { - throw new \BadMethodCallException('This method should not be called directly on the FingersCrossedHandler.'); + throw new \BadMethodCallException('This method should not be called directly on the ForwarderHandler.'); } private function initializeHandlers() From 0eb5896c5bd4657f484200c3f392ca4155b78669 Mon Sep 17 00:00:00 2001 From: lenar Date: Tue, 10 May 2011 18:04:41 +0300 Subject: [PATCH 4/6] Rename to GroupHandler, remove callback support, add couple of tests --- ...{ForwarderHandler.php => GroupHandler.php} | 0 tests/Monolog/Handler/GroupHandlerTest.php | 43 +++++++++++++++++++ 2 files changed, 43 insertions(+) rename src/Monolog/Handler/{ForwarderHandler.php => GroupHandler.php} (100%) create mode 100644 tests/Monolog/Handler/GroupHandlerTest.php diff --git a/src/Monolog/Handler/ForwarderHandler.php b/src/Monolog/Handler/GroupHandler.php similarity index 100% rename from src/Monolog/Handler/ForwarderHandler.php rename to src/Monolog/Handler/GroupHandler.php diff --git a/tests/Monolog/Handler/GroupHandlerTest.php b/tests/Monolog/Handler/GroupHandlerTest.php new file mode 100644 index 00000000..fc7a186a --- /dev/null +++ b/tests/Monolog/Handler/GroupHandlerTest.php @@ -0,0 +1,43 @@ + + * + * 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 ForwarderHandlerTest extends TestCase +{ + public function testHandleForward() + { + $testHandlers = array(new TestHandler(), new TestHandler()); + $handler = new ForwarderHandler($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); + } + } + + public function testHandleBatchForward() + { + $testHandlers = array(new TestHandler(), new TestHandler()); + $handler = new ForwarderHandler($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); + } + } +} From b6949fbf8fc9a6169c20b1d6cdaddb93a0820b0e Mon Sep 17 00:00:00 2001 From: lenar Date: Tue, 10 May 2011 18:07:42 +0300 Subject: [PATCH 5/6] Rename to GroupHandler, remove callback support, add couple of tests, try 2 --- src/Monolog/Handler/GroupHandler.php | 23 ++-------------------- tests/Monolog/Handler/GroupHandlerTest.php | 10 +++++----- 2 files changed, 7 insertions(+), 26 deletions(-) diff --git a/src/Monolog/Handler/GroupHandler.php b/src/Monolog/Handler/GroupHandler.php index d34a1d00..27eb6959 100644 --- a/src/Monolog/Handler/GroupHandler.php +++ b/src/Monolog/Handler/GroupHandler.php @@ -18,9 +18,8 @@ use Monolog\Logger; * * @author Lenar Lõhmus */ -class ForwarderHandler extends AbstractHandler +class GroupHandler extends AbstractHandler { - private $handlersInitialized; protected $handlers; /** @@ -29,7 +28,6 @@ class ForwarderHandler extends AbstractHandler */ public function __construct(array $handlers, $bubble = false) { - $this->handlersInitialized = false; $this->handlers = $handlers; $this->bubble = $bubble; } @@ -39,8 +37,6 @@ class ForwarderHandler extends AbstractHandler */ public function handle(array $record) { - $this->handlersInitialized || $this->initializeHandlers(); - foreach ($this->handlers as $handler) { $handler->handle($record); } @@ -52,8 +48,6 @@ class ForwarderHandler extends AbstractHandler */ public function handleBatch(array $records) { - $this->handlersInitialized || $this->initializeHandlers(); - foreach ($this->handlers as $handler) { $handler->handleBatch($records); } @@ -64,19 +58,6 @@ class ForwarderHandler extends AbstractHandler */ protected function write(array $record) { - throw new \BadMethodCallException('This method should not be called directly on the ForwarderHandler.'); - } - - private function initializeHandlers() - { - foreach ($this->handlers as &$handler) { - if (!$handler instanceof HandlerInterface) { - $handler = call_user_func($handler, $record, $this); - if (!$handler instanceof HandlerInterface) { - throw new \RuntimeException("The factory callback should return a HandlerInterface"); - } - } - } - $this->handlersInitialized = true; + throw new \BadMethodCallException('This method should not be called directly on the GroupHandler.'); } } diff --git a/tests/Monolog/Handler/GroupHandlerTest.php b/tests/Monolog/Handler/GroupHandlerTest.php index fc7a186a..0eae4060 100644 --- a/tests/Monolog/Handler/GroupHandlerTest.php +++ b/tests/Monolog/Handler/GroupHandlerTest.php @@ -14,12 +14,12 @@ namespace Monolog\Handler; use Monolog\TestCase; use Monolog\Logger; -class ForwarderHandlerTest extends TestCase +class GroupHandlerTest extends TestCase { - public function testHandleForward() + public function testHandle() { $testHandlers = array(new TestHandler(), new TestHandler()); - $handler = new ForwarderHandler($testHandlers); + $handler = new GroupHandler($testHandlers); $handler->handle($this->getRecord(Logger::DEBUG)); $handler->handle($this->getRecord(Logger::INFO)); foreach ($testHandlers as $test) { @@ -29,10 +29,10 @@ class ForwarderHandlerTest extends TestCase } } - public function testHandleBatchForward() + public function testHandleBatch() { $testHandlers = array(new TestHandler(), new TestHandler()); - $handler = new ForwarderHandler($testHandlers); + $handler = new GroupHandler($testHandlers); $handler->handleBatch(array($this->getRecord(Logger::DEBUG), $this->getRecord(Logger::INFO))); foreach ($testHandlers as $test) { $this->assertTrue($test->hasDebugRecords()); From e969124cec4ec1236447c53d6add1ca6ef1fcc1d Mon Sep 17 00:00:00 2001 From: lenar Date: Tue, 10 May 2011 18:13:03 +0300 Subject: [PATCH 6/6] Update documentation about GroupHandler --- README.mdown | 1 + 1 file changed, 1 insertion(+) diff --git a/README.mdown b/README.mdown index 1c19540f..1867bd70 100644 --- a/README.mdown +++ b/README.mdown @@ -47,6 +47,7 @@ Wrappers / Special Handlers - _FingersCrossedHandler_: A very interesting wrapper. It takes a logger as parameter and will accumulate log records of all levels until a record exceeds the defined severity level. At which point it delivers all records, including those of lower severity, to the handler it wraps. This means that until an error actually happens you will not see anything in your logs, but when it happens you will have the full information, including debug and info records. This provides you with all the information you need, but only when you need it. - _NullHandler_: Any record it can handle will be thrown away. This can be used to put on top of an existing handler stack to disable it temporarily. - _BufferHandler_: This handler will buffer all the log records it receives until close() is called at which point it will call handleBatch() on the handler it wraps with all the log messages at once. This is very useful to send an email with all records at once for example instead of having one mail for every log record. +- _GroupHandler_: This handler groups other handlers. Every record received is sent to all the handlers it is configured with. - _TestHandler_: Used for testing, it records everything that is sent to it and has accessors to read out the information. Formatters