Просмотр исходного кода

Improved the Raven handler when using a Buffer handler

Fabien Potencier 12 лет назад
Родитель
Сommit
29ae147226
2 измененных файлов с 109 добавлено и 0 удалено
  1. 69 0
      src/Monolog/Handler/RavenHandler.php
  2. 40 0
      tests/Monolog/Handler/RavenHandlerTest.php

+ 69 - 0
src/Monolog/Handler/RavenHandler.php

@@ -12,6 +12,7 @@
 namespace Monolog\Handler;
 
 use Monolog\Formatter\LineFormatter;
+use Monolog\Formatter\FormatterInterface;
 use Monolog\Logger;
 use Monolog\Handler\AbstractProcessingHandler;
 use Raven_Client;
@@ -43,6 +44,11 @@ class RavenHandler extends AbstractProcessingHandler
      */
     protected $ravenClient;
 
+    /**
+     * @var LineFormatter The formatter to use for the logs generated via handleBatch()
+     */
+    protected $logFormatter;
+
     /**
      * @param Raven_Client $ravenClient
      * @param integer      $level       The minimum logging level at which this handler will be triggered
@@ -55,6 +61,59 @@ class RavenHandler extends AbstractProcessingHandler
         $this->ravenClient = $ravenClient;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function handleBatch(array $records)
+    {
+        // the last records is the "main" one
+        $record = array_pop($records);
+
+        // no need to go further if the main record is not at the right level
+        if ($record['level'] < $this->level) {
+            return;
+        }
+
+        // the other ones are added as a context item
+        $logs = array();
+        foreach ($records as $r) {
+            if ($r['level'] < $this->level) {
+                continue;
+            }
+            $logs[] = $this->processRecord($r);
+        }
+
+        if ($logs) {
+            $record['context']['logs'] = (string) $this->getLogFormatter()->formatBatch($logs);
+        }
+
+        $this->handle($record);
+    }
+
+    /**
+     * Sets the formatter for the logs generated by handleBatch().
+     *
+     * @param FormatterInterface $formatter
+     */
+    public function setLogFormatter(FormatterInterface $formatter)
+    {
+        $this->logFormatter = $formatter;
+    }
+
+    /**
+     * Gets the formatter for the logs generated by handleBatch().
+     *
+     * @return FormatterInterface
+     */
+    public function getLogFormatter()
+    {
+        if (!$this->logFormatter) {
+            $this->logFormatter = $this->getDefaultLogFormatter();
+        }
+
+        return $this->logFormatter;
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -85,4 +144,14 @@ class RavenHandler extends AbstractProcessingHandler
     {
         return new LineFormatter('[%channel%] %message%');
     }
+
+    /**
+     * Gets the default formatter for the logs generated by handleBatch().
+     *
+     * @return FormatterInterface
+     */
+    protected function getDefaultLogFormatter()
+    {
+        return new LineFormatter();
+    }
 }

+ 40 - 0
tests/Monolog/Handler/RavenHandlerTest.php

@@ -13,6 +13,7 @@ namespace Monolog\Handler;
 
 use Monolog\TestCase;
 use Monolog\Logger;
+use Monolog\Formatter\LineFormatter;
 use Monolog\Handler\RavenHandler;
 
 class RavenHandlerTest extends TestCase
@@ -88,6 +89,45 @@ class RavenHandlerTest extends TestCase
         $this->assertEquals($record['message'], $ravenClient->lastData['message']);
     }
 
+    public function testHandleBatch()
+    {
+        $records = $this->getMultipleRecords();
+
+        $logFormatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
+        $logFormatter->expects($this->once())->method('formatBatch');
+
+        $formatter = $this->getMock('Monolog\\Formatter\\FormatterInterface');
+        $formatter->expects($this->once())->method('format');
+
+        $handler = $this->getHandler($this->getRavenClient());
+        $handler->setLogFormatter($logFormatter);
+        $handler->setFormatter($formatter);
+        $handler->handleBatch($records);
+    }
+
+    public function testHandleBatchDoNothingIfRecordsAreBelowLevel()
+    {
+        $records = array(
+            $this->getRecord(Logger::DEBUG, 'debug message 1'),
+            $this->getRecord(Logger::DEBUG, 'debug message 2'),
+            $this->getRecord(Logger::INFO, 'information'),
+        );
+
+        $handler = $this->getMock('Monolog\Handler\RavenHandler', null, array($this->getRavenClient()));
+        $handler->expects($this->never())->method('handle');
+        $handler->setLevel(Logger::ERROR);
+        $handler->handleBatch($records);
+    }
+
+    public function testGetSetLogFormatter()
+    {
+        $ravenClient = $this->getRavenClient();
+        $handler = $this->getHandler($ravenClient);
+
+        $handler->setLogFormatter($formatter = new LineFormatter());
+        $this->assertSame($formatter, $handler->getLogFormatter());
+    }
+
     private function methodThatThrowsAnException()
     {
         throw new \Exception('This is an exception');