Browse Source

Merge pull request #413 from fotomerchant/feature/slackEmoji

Added Emoji support for the Slack Handler
Jordi Boggiano 11 năm trước cách đây
mục cha
commit
4f85e5570c

+ 13 - 2
src/Monolog/Handler/SlackHandler.php

@@ -38,6 +38,12 @@ class SlackHandler extends SocketHandler
      * @var string
      * @var string
      */
      */
     private $username;
     private $username;
+    
+    /**
+     * Emoji icon name
+     * @var string
+     */
+    private $iconEmoji;
 
 
     /**
     /**
      * Whether the message should be added to Slack as attachment (plain text otherwise)
      * Whether the message should be added to Slack as attachment (plain text otherwise)
@@ -53,7 +59,7 @@ class SlackHandler extends SocketHandler
      * @param int    $level         The minimum logging level at which this handler will be triggered
      * @param int    $level         The minimum logging level at which this handler will be triggered
      * @param bool   $bubble        Whether the messages that are handled can bubble up the stack or not
      * @param bool   $bubble        Whether the messages that are handled can bubble up the stack or not
      */
      */
-    public function __construct($token, $channel, $username = 'Monolog', $useAttachment = true, $level = Logger::CRITICAL, $bubble = true)
+    public function __construct($token, $channel, $username = 'Monolog', $useAttachment = true, $level = Logger::CRITICAL, $bubble = true, $iconEmoji = null)
     {
     {
         if (!extension_loaded('openssl')) {
         if (!extension_loaded('openssl')) {
             throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler');
             throw new MissingExtensionException('The OpenSSL PHP extension is required to use the SlackHandler');
@@ -64,6 +70,7 @@ class SlackHandler extends SocketHandler
         $this->token = $token;
         $this->token = $token;
         $this->channel = $channel;
         $this->channel = $channel;
         $this->username = $username;
         $this->username = $username;
+        $this->iconEmoji = $iconEmoji;
         $this->useAttachment = $useAttachment;
         $this->useAttachment = $useAttachment;
     }
     }
 
 
@@ -120,7 +127,11 @@ class SlackHandler extends SocketHandler
         } else {
         } else {
             $dataArray['text'] = $record['message'];
             $dataArray['text'] = $record['message'];
         }
         }
-
+        
+        if ($this->iconEmoji !== null) {
+            $dataArray['icon_emoji'] = ":{$this->iconEmoji}:";
+        }
+        
         return http_build_query($dataArray);
         return http_build_query($dataArray);
     }
     }
 
 

+ 12 - 2
tests/Monolog/Handler/SlackHandlerTest.php

@@ -56,6 +56,16 @@ class SlackHandlerTest extends TestCase
 
 
         $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
         $this->assertRegexp('/token=myToken&channel=channel1&username=Monolog&text=&attachments=.*$/', $content);
     }
     }
+    
+    public function testWriteContentWithEmoji()
+    {
+        $this->createHandler('myToken', 'channel1', 'Monolog', true, 'alien');
+        $this->handler->handle($this->getRecord(Logger::CRITICAL, 'test1'));
+        fseek($this->res, 0);
+        $content = fread($this->res, 1024);
+
+        $this->assertRegexp('/icon_emoji=%3Aalien%3A$/', $content);
+    }
 
 
     /**
     /**
      * @dataProvider provideLevelColors
      * @dataProvider provideLevelColors
@@ -94,9 +104,9 @@ class SlackHandlerTest extends TestCase
         );
         );
     }
     }
 
 
-    private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true)
+    private function createHandler($token = 'myToken', $channel = 'channel1', $username = 'Monolog', $useAttachment = true, $iconEmoji = null)
     {
     {
-        $constructorArgs = array($token, $channel, $username, $useAttachment, Logger::DEBUG, true);
+        $constructorArgs = array($token, $channel, $username, $useAttachment, Logger::DEBUG, true, $iconEmoji);
         $this->res = fopen('php://memory', 'a');
         $this->res = fopen('php://memory', 'a');
         $this->handler = $this->getMock(
         $this->handler = $this->getMock(
             '\Monolog\Handler\SlackHandler',
             '\Monolog\Handler\SlackHandler',