Procházet zdrojové kódy

Add array of records to MailHandler::send to allow analysis of individual records before sending email

Jordi Boggiano před 13 roky
rodič
revize
5c97153efe

+ 4 - 3
src/Monolog/Handler/MailHandler.php

@@ -33,7 +33,7 @@ abstract class MailHandler extends AbstractProcessingHandler
         }
         }
 
 
         if (!empty($messages)) {
         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
      * Send a mail with the given content
      *
      *
      * @param string $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}
      * {@inheritdoc}
      */
      */
     protected function write(array $record)
     protected function write(array $record)
     {
     {
-        $this->send((string) $record['formatted']);
+        $this->send((string) $record['formatted'], array($record));
     }
     }
 }
 }

+ 1 - 1
src/Monolog/Handler/NativeMailerHandler.php

@@ -42,7 +42,7 @@ class NativeMailerHandler extends MailHandler
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    protected function send($content)
+    protected function send($content, array $records)
     {
     {
         mail($this->to, $this->subject, wordwrap($content, 70), $this->headers);
         mail($this->to, $this->subject, wordwrap($content, 70), $this->headers);
     }
     }

+ 1 - 1
src/Monolog/Handler/SwiftMailerHandler.php

@@ -45,7 +45,7 @@ class SwiftMailerHandler extends MailHandler
     /**
     /**
      * {@inheritdoc}
      * {@inheritdoc}
      */
      */
-    protected function send($content)
+    protected function send($content, array $records)
     {
     {
         $message = clone $this->message;
         $message = clone $this->message;
         $message->setBody($content);
         $message->setBody($content);

+ 8 - 2
tests/Monolog/Handler/MailHandlerTest.php

@@ -61,9 +61,15 @@ class MailHandlerTest extends TestCase
     public function testHandle()
     public function testHandle()
     {
     {
         $handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
         $handler = $this->getMockForAbstractClass('Monolog\\Handler\\MailHandler');
+
+        $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())
         $handler->expects($this->once())
-            ->method('send');
+            ->method('send')
+            ->with($records[0]['formatted'], $records);
 
 
-        $handler->handle($this->getRecord());
+        $handler->handle($record);
     }
     }
 }
 }