Browse Source

Truncate single messages if they go over the hipchat limit, fixes #629

Jordi Boggiano 9 years ago
parent
commit
ccdc8b530c

+ 8 - 0
src/Monolog/Handler/HipChatHandler.php

@@ -143,6 +143,14 @@ class HipChatHandler extends SocketHandler
             'color' => $this->getAlertColor($record['level']),
         );
 
+        if (!$this->validateStringLength($dataArray['message'], static::MAXIMUM_MESSAGE_LENGTH)) {
+            if (function_exists('mb_substr')) {
+                $dataArray['message'] = mb_substr($dataArray['message'], 0, static::MAXIMUM_MESSAGE_LENGTH).' [truncated]';
+            } else {
+                $dataArray['message'] = substr($dataArray['message'], 0, static::MAXIMUM_MESSAGE_LENGTH).' [truncated]';
+            }
+        }
+
         // if we are using the legacy API then we need to send some additional information
         if ($this->version == self::API_V1) {
             $dataArray['room_id'] = $this->room;

+ 10 - 0
tests/Monolog/Handler/HipChatHandlerTest.php

@@ -150,6 +150,16 @@ class HipChatHandlerTest extends TestCase
         $this->assertRegexp('/message=Backup\+of\+database\+%22example%22\+finished\+in\+16\+minutes\./', $content);
     }
 
+    public function testWriteTruncatesLongMessage()
+    {
+        $this->createHandler();
+        $this->handler->handle($this->getRecord(Logger::CRITICAL, str_repeat('abcde', 2000)));
+        fseek($this->res, 0);
+        $content = fread($this->res, 12000);
+
+        $this->assertRegexp('/message='.str_repeat('abcde', 1900).'\+%5Btruncated%5D/', $content);
+    }
+
     /**
      * @dataProvider provideLevelColors
      */