From f72b5036f8421968e4968e9ce905b751b78b53d7 Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 5 Apr 2016 12:17:34 +0200 Subject: [PATCH 1/4] extract browser detection regex into a constant --- src/Monolog/Handler/ChromePHPHandler.php | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Monolog/Handler/ChromePHPHandler.php b/src/Monolog/Handler/ChromePHPHandler.php index a6f30858..b00fa844 100644 --- a/src/Monolog/Handler/ChromePHPHandler.php +++ b/src/Monolog/Handler/ChromePHPHandler.php @@ -32,6 +32,11 @@ class ChromePHPHandler extends AbstractProcessingHandler * Header name */ const HEADER_NAME = 'X-ChromeLogger-Data'; + + /** + * Regular expression to detect supported browsers (matches any Chrome, or Firefox 43+) + */ + const USER_AGENT_REGEX = '{\b(?:Chrome/\d+(?:\.\d+)*|Firefox/(?:4[3-9]|[5-9]\d|\d{3,})(?:\.\d)*)\b}'; protected static $initialized = false; @@ -177,8 +182,7 @@ class ChromePHPHandler extends AbstractProcessingHandler return false; } - // matches any Chrome, or Firefox 43+ - return preg_match('{\b(?:Chrome/\d+(?:\.\d+)*|Firefox/(?:4[3-9]|[5-9]\d|\d{3,})(?:\.\d)*)\b}', $_SERVER['HTTP_USER_AGENT']); + return preg_match(self::USER_AGENT_REGEX, $_SERVER['HTTP_USER_AGENT']); } /** From b437bb928a2a95380a9a49476a841a49c4e8176a Mon Sep 17 00:00:00 2001 From: Christian Flothmann Date: Tue, 5 Apr 2016 12:35:18 +0200 Subject: [PATCH 2/4] fix Gelf message formatter tests Version 1.4.1 is the last version compatible with PHP < 5.3.9, but filtered all message attributes with zero string length. --- .../Formatter/GelfMessageFormatterTest.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/tests/Monolog/Formatter/GelfMessageFormatterTest.php b/tests/Monolog/Formatter/GelfMessageFormatterTest.php index 2052cf66..e6bd3098 100644 --- a/tests/Monolog/Formatter/GelfMessageFormatterTest.php +++ b/tests/Monolog/Formatter/GelfMessageFormatterTest.php @@ -211,8 +211,20 @@ class GelfMessageFormatterTest extends \PHPUnit_Framework_TestCase ); $message = $formatter->format($record); $messageArray = $message->toArray(); - $this->assertLessThanOrEqual(32766, strlen($messageArray['_key'])); - $this->assertLessThanOrEqual(32766, strlen($messageArray['_ctxt_exception'])); + + // 200 for padding + metadata + $length = 200; + + foreach ($messageArray as $key => $value) { + if (!in_array($key, array('level', 'timestamp'))) { + $length += strlen($value); + } + } + + // in graylog2/gelf-php before 1.4.1 empty strings are filtered and won't be included in the message + // though it should be sufficient to ensure that the entire message length does not exceed the maximum + // length being allowed + $this->assertLessThanOrEqual(32766, $length, 'The message length is no longer than the maximum allowed length'); } private function isLegacy() From 6c21baf55304dd2dfe5f173a45dd359521ed70b9 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 10 Apr 2016 12:27:44 +0100 Subject: [PATCH 3/4] Remove 5.3.3 build as it lacks TLS --- .travis.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cd0a9552..de07fa93 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,6 @@ sudo: false matrix: include: - - php: 5.3.3 - php: 5.3 - php: 5.3 env: deps=low From ccdc8b530ce328ca66b5484d60c964d5c459473c Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 10 Apr 2016 12:31:24 +0100 Subject: [PATCH 4/4] 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 */