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

Allow formatting of message by setting a line formatter on SlackHandler, fixes #829

Jordi Boggiano 9 лет назад
Родитель
Сommit
9b5bf2cca7
2 измененных файлов с 28 добавлено и 4 удалено
  1. 10 4
      src/Monolog/Handler/SlackHandler.php
  2. 18 0
      tests/Monolog/Handler/SlackHandlerTest.php

+ 10 - 4
src/Monolog/Handler/SlackHandler.php

@@ -144,19 +144,25 @@ class SlackHandler extends SocketHandler
             'attachments' => array(),
         );
 
+        if ($this->formatter) {
+            $message = $this->formatter->format($record);
+        } else {
+            $message = $record['message'];
+        }
+
         if ($this->useAttachment) {
             $attachment = array(
-                'fallback' => $record['message'],
+                'fallback' => $message,
                 'color'    => $this->getAttachmentColor($record['level']),
                 'fields'   => array(),
             );
 
             if ($this->useShortAttachment) {
                 $attachment['title'] = $record['level_name'];
-                $attachment['text'] = $record['message'];
+                $attachment['text'] = $message;
             } else {
                 $attachment['title'] = 'Message';
-                $attachment['text'] = $record['message'];
+                $attachment['text'] = $message;
                 $attachment['fields'][] = array(
                     'title' => 'Level',
                     'value' => $record['level_name'],
@@ -206,7 +212,7 @@ class SlackHandler extends SocketHandler
 
             $dataArray['attachments'] = json_encode(array($attachment));
         } else {
-            $dataArray['text'] = $record['message'];
+            $dataArray['text'] = $message;
         }
 
         if ($this->iconEmoji) {

+ 18 - 0
tests/Monolog/Handler/SlackHandlerTest.php

@@ -13,6 +13,7 @@ namespace Monolog\Handler;
 
 use Monolog\TestCase;
 use Monolog\Logger;
+use Monolog\Formatter\LineFormatter;
 
 /**
  * @author Greg Kedzierski <greg@gregkedzierski.com>
@@ -57,6 +58,23 @@ class SlackHandlerTest extends TestCase
         $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
     }
 
+    public function testWriteContentUsesFormatterIfProvided()
+    {
+        $this->createHandler('myToken', 'channel1', 'Monolog', false);
+        $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
+        fseek($this->res, 0);
+        $content = fread($this->res, 1024);
+
+        $this->createHandler('myToken', 'channel1', 'Monolog', false);
+        $this->handler->setFormatter(new LineFormatter('foo--%message%'));
+        $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test2'));
+        fseek($this->res, 0);
+        $content2 = fread($this->res, 1024);
+
+        $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=test1.*$/', $content);
+        $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=foo--test2.*$/', $content2);
+    }
+
     public function testWriteContentWithEmoji()
     {
         $this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');