From faed450d52d7e64ca26ce9023694e6c5e0f87bf4 Mon Sep 17 00:00:00 2001 From: Enleur Date: Fri, 7 Apr 2017 13:22:14 +0300 Subject: [PATCH 1/4] Use GELF max length per field and make max length configurable in constructor --- .../Formatter/GelfMessageFormatter.php | 33 ++++++++++--------- .../Formatter/GelfMessageFormatterTest.php | 32 +++++++++++++++--- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/Monolog/Formatter/GelfMessageFormatter.php b/src/Monolog/Formatter/GelfMessageFormatter.php index 64e76652..bfdfcc08 100644 --- a/src/Monolog/Formatter/GelfMessageFormatter.php +++ b/src/Monolog/Formatter/GelfMessageFormatter.php @@ -22,7 +22,7 @@ use Gelf\Message; */ class GelfMessageFormatter extends NormalizerFormatter { - const MAX_LENGTH = 32766; + const DEFAULT_MAX_LENGTH = 32766; /** * @var string the name of the system for the Gelf log message @@ -39,6 +39,11 @@ class GelfMessageFormatter extends NormalizerFormatter */ protected $contextPrefix; + /** + * @var int max length per field + */ + protected $maxLength; + /** * Translates Monolog log levels to Graylog2 log priorities. */ @@ -53,7 +58,7 @@ class GelfMessageFormatter extends NormalizerFormatter Logger::EMERGENCY => 0, ); - public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_') + public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_', $maxLength = self::DEFAULT_MAX_LENGTH) { parent::__construct('U.u'); @@ -61,6 +66,7 @@ class GelfMessageFormatter extends NormalizerFormatter $this->extraPrefix = $extraPrefix; $this->contextPrefix = $contextPrefix; + $this->maxLength = $maxLength; } /** @@ -81,35 +87,30 @@ class GelfMessageFormatter extends NormalizerFormatter ->setHost($this->systemName) ->setLevel($this->logLevels[$record['level']]); - // start count with message length + system name length + 200 for padding / metadata + // message length + system name length + 200 for padding / metadata $len = 200 + strlen((string) $record['message']) + strlen($this->systemName); - if ($len > self::MAX_LENGTH) { - $message->setShortMessage(substr($record['message'], 0, self::MAX_LENGTH - 200)); - - return $message; + if ($len > $this->maxLength) { + $message->setShortMessage(substr($record['message'], 0, $this->maxLength)); } if (isset($record['channel'])) { $message->setFacility($record['channel']); - $len += strlen($record['channel']); } if (isset($record['extra']['line'])) { $message->setLine($record['extra']['line']); - $len += 10; unset($record['extra']['line']); } if (isset($record['extra']['file'])) { $message->setFile($record['extra']['file']); - $len += strlen($record['extra']['file']); unset($record['extra']['file']); } foreach ($record['extra'] as $key => $val) { $val = is_scalar($val) || null === $val ? $val : $this->toJson($val); - $len += strlen($this->extraPrefix . $key . $val); - if ($len > self::MAX_LENGTH) { - $message->setAdditional($this->extraPrefix . $key, substr($val, 0, self::MAX_LENGTH - $len)); + $len = strlen($this->extraPrefix . $key . $val); + if ($len > $this->maxLength) { + $message->setAdditional($this->extraPrefix . $key, substr($val, 0, $this->maxLength)); break; } $message->setAdditional($this->extraPrefix . $key, $val); @@ -117,9 +118,9 @@ class GelfMessageFormatter extends NormalizerFormatter foreach ($record['context'] as $key => $val) { $val = is_scalar($val) || null === $val ? $val : $this->toJson($val); - $len += strlen($this->contextPrefix . $key . $val); - if ($len > self::MAX_LENGTH) { - $message->setAdditional($this->contextPrefix . $key, substr($val, 0, self::MAX_LENGTH - $len)); + $len = strlen($this->contextPrefix . $key . $val); + if ($len > $this->maxLength) { + $message->setAdditional($this->contextPrefix . $key, substr($val, 0, $this->maxLength)); break; } $message->setAdditional($this->contextPrefix . $key, $val); diff --git a/tests/Monolog/Formatter/GelfMessageFormatterTest.php b/tests/Monolog/Formatter/GelfMessageFormatterTest.php index e6bd3098..889dc542 100644 --- a/tests/Monolog/Formatter/GelfMessageFormatterTest.php +++ b/tests/Monolog/Formatter/GelfMessageFormatterTest.php @@ -221,10 +221,34 @@ class GelfMessageFormatterTest extends \PHPUnit_Framework_TestCase } } - // 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'); + $this->assertLessThanOrEqual(65792, $length, 'The message length is no longer than the maximum allowed length'); + } + + public function testFormatWithUnlimitedLength() + { + $formatter = new GelfMessageFormatter(null, null, 'ctxt_', PHP_INT_MAX); + $record = array( + 'level' => Logger::ERROR, + 'level_name' => 'ERROR', + 'channel' => 'meh', + 'context' => array('exception' => str_repeat(' ', 32767 * 2)), + 'datetime' => new \DateTime("@0"), + 'extra' => array('key' => str_repeat(' ', 32767 * 2)), + 'message' => 'log' + ); + $message = $formatter->format($record); + $messageArray = $message->toArray(); + + // 200 for padding + metadata + $length = 200; + + foreach ($messageArray as $key => $value) { + if (!in_array($key, array('level', 'timestamp'))) { + $length += strlen($value); + } + } + + $this->assertGreaterThanOrEqual(131289, $length, 'The message should not be truncated'); } private function isLegacy() From 1b660bbbeedfa4681a2cabd84b6f852538036b29 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 10 Apr 2017 09:37:23 +0200 Subject: [PATCH 2/4] Remove constant from constructor --- src/Monolog/Formatter/GelfMessageFormatter.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Monolog/Formatter/GelfMessageFormatter.php b/src/Monolog/Formatter/GelfMessageFormatter.php index bfdfcc08..2c1b0e86 100644 --- a/src/Monolog/Formatter/GelfMessageFormatter.php +++ b/src/Monolog/Formatter/GelfMessageFormatter.php @@ -58,7 +58,7 @@ class GelfMessageFormatter extends NormalizerFormatter Logger::EMERGENCY => 0, ); - public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_', $maxLength = self::DEFAULT_MAX_LENGTH) + public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_', $maxLength = null) { parent::__construct('U.u'); @@ -66,7 +66,7 @@ class GelfMessageFormatter extends NormalizerFormatter $this->extraPrefix = $extraPrefix; $this->contextPrefix = $contextPrefix; - $this->maxLength = $maxLength; + $this->maxLength = is_null($maxLength) ? self::DEFAULT_MAX_LENGTH : $maxLength; } /** From 6696e045dfef525d0b6459021d81f9a892f1f1f9 Mon Sep 17 00:00:00 2001 From: Lesnykh Ilia Date: Wed, 3 May 2017 15:37:25 +0300 Subject: [PATCH 3/4] Update JsonFormatter.php --- src/Monolog/Formatter/JsonFormatter.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Monolog/Formatter/JsonFormatter.php b/src/Monolog/Formatter/JsonFormatter.php index 4b2be77d..0782f149 100644 --- a/src/Monolog/Formatter/JsonFormatter.php +++ b/src/Monolog/Formatter/JsonFormatter.php @@ -28,6 +28,7 @@ class JsonFormatter extends NormalizerFormatter protected $batchMode; protected $appendNewline; + /** * @var bool */ @@ -35,6 +36,7 @@ class JsonFormatter extends NormalizerFormatter /** * @param int $batchMode + * @param bool $appendNewline */ public function __construct($batchMode = self::BATCH_MODE_JSON, $appendNewline = true) { From 70ed794fb2fefd60d44ac0694e0e0a2b5e952043 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sun, 18 Jun 2017 23:42:01 +0200 Subject: [PATCH 4/4] Update to trusty builds --- .travis.yml | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0b053f5b..7a8c0c27 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,19 +1,25 @@ language: php sudo: false +dist: trusty + +php: + - 5.4 + - 5.5 + - 5.6 + - 7.0 + - 7.1 + - hhvm + - nightly matrix: - include: - - php: 5.3 - - php: 5.3 - env: deps=low - - php: 5.4 - - php: 5.5 - - php: 5.6 - - php: 7 - - php: 7.1 - - php: hhvm - fast_finish: true + include: + - dist: precise + php: 5.3 + - dist: precise + php: 5.3 + env: deps=low + fast_finish: true before_script: - if [[ $TRAVIS_PHP_VERSION = 5.* ]]; then echo "extension = mongo.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini; fi