From ccdc8b530ce328ca66b5484d60c964d5c459473c Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 10 Apr 2016 12:31:24 +0100 Subject: [PATCH] Truncate single messages if they go over the hipchat limit, fixes #629 --- src/Monolog/Handler/HipChatHandler.php | 8 ++++++++ tests/Monolog/Handler/HipChatHandlerTest.php | 10 ++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/Monolog/Handler/HipChatHandler.php b/src/Monolog/Handler/HipChatHandler.php index 9807df1c..73049f36 100644 --- a/src/Monolog/Handler/HipChatHandler.php +++ b/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; diff --git a/tests/Monolog/Handler/HipChatHandlerTest.php b/tests/Monolog/Handler/HipChatHandlerTest.php index e45f57de..52dc9dac 100644 --- a/tests/Monolog/Handler/HipChatHandlerTest.php +++ b/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 */