1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-07-30 09:50:26 +02:00

use mb_ functions in GelfFormatter

This commit is contained in:
vershinin_so
2019-02-14 10:32:48 +03:00
parent bf486002a0
commit ada57dd4a1
2 changed files with 26 additions and 6 deletions

View File

@@ -93,10 +93,10 @@ class GelfMessageFormatter extends NormalizerFormatter
->setLevel($this->logLevels[$record['level']]);
// message length + system name length + 200 for padding / metadata
$len = 200 + strlen((string) $record['message']) + strlen($this->systemName);
$len = 200 + mb_strlen((string) $record['message']) + mb_strlen($this->systemName);
if ($len > $this->maxLength) {
$message->setShortMessage(substr($record['message'], 0, $this->maxLength));
$message->setShortMessage(mb_substr($record['message'], 0, $this->maxLength));
}
if (isset($record['channel'])) {
@@ -113,9 +113,9 @@ class GelfMessageFormatter extends NormalizerFormatter
foreach ($record['extra'] as $key => $val) {
$val = is_scalar($val) || null === $val ? $val : $this->toJson($val);
$len = strlen($this->extraPrefix . $key . $val);
$len = mb_strlen($this->extraPrefix . $key . $val);
if ($len > $this->maxLength) {
$message->setAdditional($this->extraPrefix . $key, substr($val, 0, $this->maxLength));
$message->setAdditional($this->extraPrefix . $key, mb_substr($val, 0, $this->maxLength));
continue;
}
$message->setAdditional($this->extraPrefix . $key, $val);
@@ -123,9 +123,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);
$len = mb_strlen($this->contextPrefix . $key . $val);
if ($len > $this->maxLength) {
$message->setAdditional($this->contextPrefix . $key, substr($val, 0, $this->maxLength));
$message->setAdditional($this->contextPrefix . $key, mb_substr($val, 0, $this->maxLength));
continue;
}
$message->setAdditional($this->contextPrefix . $key, $val);

View File

@@ -251,6 +251,26 @@ class GelfMessageFormatterTest extends \PHPUnit\Framework\TestCase
$this->assertGreaterThanOrEqual(131289, $length, 'The message should not be truncated');
}
public function testFormatWithLargeCyrillicData()
{
$formatter = new GelfMessageFormatter();
$record = [
'level' => Logger::ERROR,
'level_name' => 'ERROR',
'channel' => 'meh',
'context' => ['exception' => str_repeat('а', 32767)],
'datetime' => new \DateTimeImmutable("@0"),
'extra' => ['key' => str_repeat('б', 32767)],
'message' => str_repeat('в', 32767),
];
$message = $formatter->format($record);
$messageArray = $message->toArray();
$messageString = json_encode($messageArray);
$this->assertIsString($messageString);
}
private function isLegacy()
{
return interface_exists('\Gelf\IMessagePublisher');