| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php declare(strict_types=1);
- /*
- * This file is part of the Monolog package.
- *
- * (c) Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Monolog\Formatter;
- use Monolog\Level;
- use Monolog\Test\MonologTestCase;
- class GelfMessageFormatterTest extends MonologTestCase
- {
- public function setUp(): void
- {
- if (!class_exists('\Gelf\Message')) {
- $this->markTestSkipped("graylog2/gelf-php is not installed");
- }
- }
- /**
- * @covers Monolog\Formatter\GelfMessageFormatter::format
- */
- public function testDefaultFormatter()
- {
- $formatter = new GelfMessageFormatter();
- $record = $this->getRecord(
- Level::Error,
- 'log',
- channel: 'meh',
- datetime: new \DateTimeImmutable("@0"),
- );
- $message = $formatter->format($record);
- $this->assertInstanceOf('Gelf\Message', $message);
- $this->assertEquals(0, $message->getTimestamp());
- $this->assertEquals('log', $message->getShortMessage());
- $this->assertEquals('meh', $message->getAdditional('facility'));
- $this->assertEquals(false, $message->hasAdditional('line'));
- $this->assertEquals(false, $message->hasAdditional('file'));
- $this->assertEquals($this->isLegacy() ? 3 : 'error', $message->getLevel());
- $this->assertNotEmpty($message->getHost());
- $formatter = new GelfMessageFormatter('mysystem');
- $message = $formatter->format($record);
- $this->assertInstanceOf('Gelf\Message', $message);
- $this->assertEquals('mysystem', $message->getHost());
- }
- /**
- * @covers Monolog\Formatter\GelfMessageFormatter::format
- */
- public function testFormatWithFileAndLine()
- {
- $formatter = new GelfMessageFormatter();
- $record = $this->getRecord(
- Level::Error,
- 'log',
- channel: 'meh',
- context: ['from' => 'logger'],
- datetime: new \DateTimeImmutable("@0"),
- extra: ['file' => 'test', 'line' => 14, 0 => 'foo'],
- );
- $message = $formatter->format($record);
- $this->assertInstanceOf('Gelf\Message', $message);
- $this->assertEquals('test', $message->getAdditional('file'));
- $this->assertEquals(14, $message->getAdditional('line'));
- }
- /**
- * @covers Monolog\Formatter\GelfMessageFormatter::format
- */
- public function testFormatWithContext()
- {
- $formatter = new GelfMessageFormatter();
- $record = $this->getRecord(
- Level::Error,
- 'log',
- channel: 'meh',
- context: ['from' => 'logger', 'trueBool' => true, 'falseBool' => false],
- datetime: new \DateTimeImmutable("@0"),
- extra: ['key' => 'pair'],
- );
- $message = $formatter->format($record);
- $this->assertInstanceOf('Gelf\Message', $message);
- $message_array = $message->toArray();
- $this->assertArrayHasKey('_ctxt_from', $message_array);
- $this->assertEquals('logger', $message_array['_ctxt_from']);
- $this->assertArrayHasKey('_ctxt_trueBool', $message_array);
- $this->assertEquals(1, $message_array['_ctxt_trueBool']);
- $this->assertArrayHasKey('_ctxt_falseBool', $message_array);
- $this->assertEquals(0, $message_array['_ctxt_falseBool']);
- // Test with extraPrefix
- $formatter = new GelfMessageFormatter(null, null, 'CTX');
- $message = $formatter->format($record);
- $this->assertInstanceOf('Gelf\Message', $message);
- $message_array = $message->toArray();
- $this->assertArrayHasKey('_CTXfrom', $message_array);
- $this->assertEquals('logger', $message_array['_CTXfrom']);
- $this->assertArrayHasKey('_CTXtrueBool', $message_array);
- $this->assertEquals(1, $message_array['_CTXtrueBool']);
- $this->assertArrayHasKey('_CTXfalseBool', $message_array);
- $this->assertEquals(0, $message_array['_CTXfalseBool']);
- }
- /**
- * @covers Monolog\Formatter\GelfMessageFormatter::format
- */
- public function testFormatWithContextContainingException()
- {
- $formatter = new GelfMessageFormatter();
- $record = $this->getRecord(
- Level::Error,
- 'log',
- channel: 'meh',
- context: ['from' => 'logger', 'exception' => [
- 'class' => '\Exception',
- 'file' => '/some/file/in/dir.php:56',
- 'trace' => ['/some/file/1.php:23', '/some/file/2.php:3'],
- ]],
- datetime: new \DateTimeImmutable("@0"),
- );
- $message = $formatter->format($record);
- $this->assertInstanceOf('Gelf\Message', $message);
- $this->assertEquals("/some/file/in/dir.php", $message->getAdditional('file'));
- $this->assertEquals("56", $message->getAdditional('line'));
- }
- /**
- * @covers Monolog\Formatter\GelfMessageFormatter::format
- */
- public function testFormatWithExtra()
- {
- $formatter = new GelfMessageFormatter();
- $record = $this->getRecord(
- Level::Error,
- 'log',
- channel: 'meh',
- context: ['from' => 'logger'],
- datetime: new \DateTimeImmutable("@0"),
- extra: ['key' => 'pair', 'trueBool' => true, 'falseBool' => false],
- );
- $message = $formatter->format($record);
- $this->assertInstanceOf('Gelf\Message', $message);
- $message_array = $message->toArray();
- $this->assertArrayHasKey('_key', $message_array);
- $this->assertEquals('pair', $message_array['_key']);
- $this->assertArrayHasKey('_trueBool', $message_array);
- $this->assertEquals(1, $message_array['_trueBool']);
- $this->assertArrayHasKey('_falseBool', $message_array);
- $this->assertEquals(0, $message_array['_falseBool']);
- // Test with extraPrefix
- $formatter = new GelfMessageFormatter(null, 'EXT');
- $message = $formatter->format($record);
- $this->assertInstanceOf('Gelf\Message', $message);
- $message_array = $message->toArray();
- $this->assertArrayHasKey('_EXTkey', $message_array);
- $this->assertEquals('pair', $message_array['_EXTkey']);
- $this->assertArrayHasKey('_EXTtrueBool', $message_array);
- $this->assertEquals(1, $message_array['_EXTtrueBool']);
- $this->assertArrayHasKey('_EXTfalseBool', $message_array);
- $this->assertEquals(0, $message_array['_EXTfalseBool']);
- }
- public function testFormatWithLargeData()
- {
- $formatter = new GelfMessageFormatter();
- $record = $this->getRecord(
- Level::Error,
- 'log',
- channel: 'meh',
- context: ['exception' => str_repeat(' ', 32767)],
- datetime: new \DateTimeImmutable("@0"),
- extra: ['key' => str_repeat(' ', 32767)],
- );
- $message = $formatter->format($record);
- $messageArray = $message->toArray();
- // 200 for padding + metadata
- $length = 200;
- foreach ($messageArray as $key => $value) {
- if (!\in_array($key, ['level', 'timestamp']) && \is_string($value)) {
- $length += \strlen($value);
- }
- }
- $this->assertLessThanOrEqual(65792, $length, 'The message length is no longer than the maximum allowed length');
- }
- public function testFormatWithUnlimitedLength()
- {
- $formatter = new GelfMessageFormatter('LONG_SYSTEM_NAME', null, 'ctxt_', PHP_INT_MAX);
- $record = $this->getRecord(
- Level::Error,
- 'log',
- channel: 'meh',
- context: ['exception' => str_repeat(' ', 32767 * 2)],
- datetime: new \DateTimeImmutable("@0"),
- extra: ['key' => str_repeat(' ', 32767 * 2)],
- );
- $message = $formatter->format($record);
- $messageArray = $message->toArray();
- // 200 for padding + metadata
- $length = 200;
- foreach ($messageArray as $key => $value) {
- if (!\in_array($key, ['level', 'timestamp'])) {
- $length += \strlen($value);
- }
- }
- $this->assertGreaterThanOrEqual(131289, $length, 'The message should not be truncated');
- }
- public function testFormatWithLargeCyrillicData()
- {
- $formatter = new GelfMessageFormatter();
- $record = $this->getRecord(
- Level::Error,
- str_repeat('в', 32767),
- channel: 'meh',
- context: ['exception' => str_repeat('а', 32767)],
- datetime: new \DateTimeImmutable("@0"),
- extra: ['key' => 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');
- }
- }
|