2
0

GelfMessageFormatterTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Formatter;
  11. use Monolog\Level;
  12. use Monolog\Test\TestCase;
  13. class GelfMessageFormatterTest extends TestCase
  14. {
  15. public function setUp(): void
  16. {
  17. if (!class_exists('\Gelf\Message')) {
  18. $this->markTestSkipped("graylog2/gelf-php is not installed");
  19. }
  20. }
  21. /**
  22. * @covers Monolog\Formatter\GelfMessageFormatter::format
  23. */
  24. public function testDefaultFormatter()
  25. {
  26. $formatter = new GelfMessageFormatter();
  27. $record = $this->getRecord(
  28. Level::Error,
  29. 'log',
  30. channel: 'meh',
  31. datetime: new \DateTimeImmutable("@0"),
  32. );
  33. $message = $formatter->format($record);
  34. $this->assertInstanceOf('Gelf\Message', $message);
  35. $this->assertEquals(0, $message->getTimestamp());
  36. $this->assertEquals('log', $message->getShortMessage());
  37. $this->assertEquals('meh', $message->getAdditional('facility'));
  38. $this->assertEquals(false, $message->hasAdditional('line'));
  39. $this->assertEquals(false, $message->hasAdditional('file'));
  40. $this->assertEquals($this->isLegacy() ? 3 : 'error', $message->getLevel());
  41. $this->assertNotEmpty($message->getHost());
  42. $formatter = new GelfMessageFormatter('mysystem');
  43. $message = $formatter->format($record);
  44. $this->assertInstanceOf('Gelf\Message', $message);
  45. $this->assertEquals('mysystem', $message->getHost());
  46. }
  47. /**
  48. * @covers Monolog\Formatter\GelfMessageFormatter::format
  49. */
  50. public function testFormatWithFileAndLine()
  51. {
  52. $formatter = new GelfMessageFormatter();
  53. $record = $this->getRecord(
  54. Level::Error,
  55. 'log',
  56. channel: 'meh',
  57. context: ['from' => 'logger'],
  58. datetime: new \DateTimeImmutable("@0"),
  59. extra: ['file' => 'test', 'line' => 14],
  60. );
  61. $message = $formatter->format($record);
  62. $this->assertInstanceOf('Gelf\Message', $message);
  63. $this->assertEquals('test', $message->getAdditional('file'));
  64. $this->assertEquals(14, $message->getAdditional('line'));
  65. }
  66. /**
  67. * @covers Monolog\Formatter\GelfMessageFormatter::format
  68. */
  69. public function testFormatWithContext()
  70. {
  71. $formatter = new GelfMessageFormatter();
  72. $record = $this->getRecord(
  73. Level::Error,
  74. 'log',
  75. channel: 'meh',
  76. context: ['from' => 'logger'],
  77. datetime: new \DateTimeImmutable("@0"),
  78. extra: ['key' => 'pair'],
  79. );
  80. $message = $formatter->format($record);
  81. $this->assertInstanceOf('Gelf\Message', $message);
  82. $message_array = $message->toArray();
  83. $this->assertArrayHasKey('_ctxt_from', $message_array);
  84. $this->assertEquals('logger', $message_array['_ctxt_from']);
  85. // Test with extraPrefix
  86. $formatter = new GelfMessageFormatter(null, null, 'CTX');
  87. $message = $formatter->format($record);
  88. $this->assertInstanceOf('Gelf\Message', $message);
  89. $message_array = $message->toArray();
  90. $this->assertArrayHasKey('_CTXfrom', $message_array);
  91. $this->assertEquals('logger', $message_array['_CTXfrom']);
  92. }
  93. /**
  94. * @covers Monolog\Formatter\GelfMessageFormatter::format
  95. */
  96. public function testFormatWithContextContainingException()
  97. {
  98. $formatter = new GelfMessageFormatter();
  99. $record = $this->getRecord(
  100. Level::Error,
  101. 'log',
  102. channel: 'meh',
  103. context: ['from' => 'logger', 'exception' => [
  104. 'class' => '\Exception',
  105. 'file' => '/some/file/in/dir.php:56',
  106. 'trace' => ['/some/file/1.php:23', '/some/file/2.php:3'],
  107. ]],
  108. datetime: new \DateTimeImmutable("@0"),
  109. );
  110. $message = $formatter->format($record);
  111. $this->assertInstanceOf('Gelf\Message', $message);
  112. $this->assertEquals("/some/file/in/dir.php", $message->getAdditional('file'));
  113. $this->assertEquals("56", $message->getAdditional('line'));
  114. }
  115. /**
  116. * @covers Monolog\Formatter\GelfMessageFormatter::format
  117. */
  118. public function testFormatWithExtra()
  119. {
  120. $formatter = new GelfMessageFormatter();
  121. $record = $this->getRecord(
  122. Level::Error,
  123. 'log',
  124. channel: 'meh',
  125. context: ['from' => 'logger'],
  126. datetime: new \DateTimeImmutable("@0"),
  127. extra: ['key' => 'pair'],
  128. );
  129. $message = $formatter->format($record);
  130. $this->assertInstanceOf('Gelf\Message', $message);
  131. $message_array = $message->toArray();
  132. $this->assertArrayHasKey('_key', $message_array);
  133. $this->assertEquals('pair', $message_array['_key']);
  134. // Test with extraPrefix
  135. $formatter = new GelfMessageFormatter(null, 'EXT');
  136. $message = $formatter->format($record);
  137. $this->assertInstanceOf('Gelf\Message', $message);
  138. $message_array = $message->toArray();
  139. $this->assertArrayHasKey('_EXTkey', $message_array);
  140. $this->assertEquals('pair', $message_array['_EXTkey']);
  141. }
  142. public function testFormatWithLargeData()
  143. {
  144. $formatter = new GelfMessageFormatter();
  145. $record = $this->getRecord(
  146. Level::Error,
  147. 'log',
  148. channel: 'meh',
  149. context: ['exception' => str_repeat(' ', 32767)],
  150. datetime: new \DateTimeImmutable("@0"),
  151. extra: ['key' => str_repeat(' ', 32767)],
  152. );
  153. $message = $formatter->format($record);
  154. $messageArray = $message->toArray();
  155. // 200 for padding + metadata
  156. $length = 200;
  157. foreach ($messageArray as $key => $value) {
  158. if (!\in_array($key, ['level', 'timestamp']) && \is_string($value)) {
  159. $length += \strlen($value);
  160. }
  161. }
  162. $this->assertLessThanOrEqual(65792, $length, 'The message length is no longer than the maximum allowed length');
  163. }
  164. public function testFormatWithUnlimitedLength()
  165. {
  166. $formatter = new GelfMessageFormatter('LONG_SYSTEM_NAME', null, 'ctxt_', PHP_INT_MAX);
  167. $record = $this->getRecord(
  168. Level::Error,
  169. 'log',
  170. channel: 'meh',
  171. context: ['exception' => str_repeat(' ', 32767 * 2)],
  172. datetime: new \DateTimeImmutable("@0"),
  173. extra: ['key' => str_repeat(' ', 32767 * 2)],
  174. );
  175. $message = $formatter->format($record);
  176. $messageArray = $message->toArray();
  177. // 200 for padding + metadata
  178. $length = 200;
  179. foreach ($messageArray as $key => $value) {
  180. if (!\in_array($key, ['level', 'timestamp'])) {
  181. $length += \strlen($value);
  182. }
  183. }
  184. $this->assertGreaterThanOrEqual(131289, $length, 'The message should not be truncated');
  185. }
  186. public function testFormatWithLargeCyrillicData()
  187. {
  188. $formatter = new GelfMessageFormatter();
  189. $record = $this->getRecord(
  190. Level::Error,
  191. str_repeat('в', 32767),
  192. channel: 'meh',
  193. context: ['exception' => str_repeat('а', 32767)],
  194. datetime: new \DateTimeImmutable("@0"),
  195. extra: ['key' => str_repeat('б', 32767)],
  196. );
  197. $message = $formatter->format($record);
  198. $messageArray = $message->toArray();
  199. $messageString = json_encode($messageArray);
  200. $this->assertIsString($messageString);
  201. }
  202. private function isLegacy()
  203. {
  204. return interface_exists('\Gelf\IMessagePublisher');
  205. }
  206. }