From 29ae1472269cc50cea82b67ec6a3a5f6e8d28645 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sat, 15 Jun 2013 15:16:38 +0200 Subject: [PATCH] Improved the Raven handler when using a Buffer handler --- src/Monolog/Handler/RavenHandler.php | 69 ++++++++++++++++++++++ tests/Monolog/Handler/RavenHandlerTest.php | 40 +++++++++++++ 2 files changed, 109 insertions(+) diff --git a/src/Monolog/Handler/RavenHandler.php b/src/Monolog/Handler/RavenHandler.php index d6c58441..66207e84 100644 --- a/src/Monolog/Handler/RavenHandler.php +++ b/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(); + } } diff --git a/tests/Monolog/Handler/RavenHandlerTest.php b/tests/Monolog/Handler/RavenHandlerTest.php index cd8ae574..9b548c2e 100644 --- a/tests/Monolog/Handler/RavenHandlerTest.php +++ b/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');