Przeglądaj źródła

Merge pull request #426 from acdameli/master

New Handler: WhatFailureGroupHandler
Jordi Boggiano 11 lat temu
rodzic
commit
0d09e2f8a0

+ 4 - 0
README.mdown

@@ -184,6 +184,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
 ----------

+ 25 - 0
src/Monolog/Handler/ExceptionTestHandler.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace Monolog\Handler;
+
+use Monolog\Logger;
+
+/**
+ * Used for testing purposes.
+ *
+ * It records all records and gives you access to them for verification. It
+ * throws an exception from handle and handleBatch to test the
+ * WhatFailureGroupHandler Class.
+ *
+ * @author Craig D'Amelio <craig@damelio.ca>
+ */
+class ExceptionTestHandler extends TestHandler
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function handle(array $record) {
+        $return = parent::handle($record);
+        throw new \Exception("ExceptionTestHandler::handle");
+    }
+}

+ 48 - 0
src/Monolog/Handler/WhatFailureGroupHandler.php

@@ -0,0 +1,48 @@
+<?php
+
+namespace Monolog\Handler;
+
+/**
+ * Forwards records to multiple handlers suppressing failures of each handler 
+ * and continuing through to give every handler a chance to succeed.
+ *
+ * @author Craig D'Amelio <craig@damelio.ca>
+ */
+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?
+            }
+        }
+    }
+}

+ 98 - 0
tests/Monolog/Handler/WhatFailureGroupHandlerTest.php

@@ -0,0 +1,98 @@
+<?php
+
+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']);
+    }
+}