From 5c97153efeabe76551d241b3b76820ae3ab5987d Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 22 Apr 2012 18:25:08 +0200 Subject: [PATCH] Add array of records to MailHandler::send to allow analysis of individual records before sending email --- src/Monolog/Handler/MailHandler.php | 7 ++++--- src/Monolog/Handler/NativeMailerHandler.php | 2 +- src/Monolog/Handler/SwiftMailerHandler.php | 2 +- tests/Monolog/Handler/MailHandlerTest.php | 12 +++++++++--- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Monolog/Handler/MailHandler.php b/src/Monolog/Handler/MailHandler.php index 9e9ab563..94ed8410 100644 --- a/src/Monolog/Handler/MailHandler.php +++ b/src/Monolog/Handler/MailHandler.php @@ -33,7 +33,7 @@ abstract class MailHandler extends AbstractProcessingHandler } if (!empty($messages)) { - $this->send((string) $this->getFormatter()->formatBatch($messages)); + $this->send((string) $this->getFormatter()->formatBatch($messages), $messages); } } @@ -41,14 +41,15 @@ abstract class MailHandler extends AbstractProcessingHandler * Send a mail with the given content * * @param string $content + * @param array $records the array of log records that formed this content */ - abstract protected function send($content); + abstract protected function send($content, array $records); /** * {@inheritdoc} */ protected function write(array $record) { - $this->send((string) $record['formatted']); + $this->send((string) $record['formatted'], array($record)); } } \ No newline at end of file diff --git a/src/Monolog/Handler/NativeMailerHandler.php b/src/Monolog/Handler/NativeMailerHandler.php index 56b76662..6933ff6a 100644 --- a/src/Monolog/Handler/NativeMailerHandler.php +++ b/src/Monolog/Handler/NativeMailerHandler.php @@ -42,7 +42,7 @@ class NativeMailerHandler extends MailHandler /** * {@inheritdoc} */ - protected function send($content) + protected function send($content, array $records) { mail($this->to, $this->subject, wordwrap($content, 70), $this->headers); } diff --git a/src/Monolog/Handler/SwiftMailerHandler.php b/src/Monolog/Handler/SwiftMailerHandler.php index e6fef494..addc4c4a 100644 --- a/src/Monolog/Handler/SwiftMailerHandler.php +++ b/src/Monolog/Handler/SwiftMailerHandler.php @@ -45,7 +45,7 @@ class SwiftMailerHandler extends MailHandler /** * {@inheritdoc} */ - protected function send($content) + protected function send($content, array $records) { $message = clone $this->message; $message->setBody($content); diff --git a/tests/Monolog/Handler/MailHandlerTest.php b/tests/Monolog/Handler/MailHandlerTest.php index 5ed3c1e4..c8d9ef5e 100644 --- a/tests/Monolog/Handler/MailHandlerTest.php +++ b/tests/Monolog/Handler/MailHandlerTest.php @@ -61,9 +61,15 @@ class MailHandlerTest extends TestCase public function testHandle() { $handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler'); - $handler->expects($this->once()) - ->method('send'); - $handler->handle($this->getRecord()); + $record = $this->getRecord(); + $records = array($record); + $records[0]['formatted'] = '['.$record['datetime']->format('Y-m-d H:i:s').'] test.WARNING: test [] []'."\n"; + + $handler->expects($this->once()) + ->method('send') + ->with($records[0]['formatted'], $records); + + $handler->handle($record); } } \ No newline at end of file