GelfMessageFormatterTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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\MonologTestCase;
  13. class GelfMessageFormatterTest extends MonologTestCase
  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, 0 => 'foo'],
  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', 'trueBool' => true, 'falseBool' => false],
  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. $this->assertArrayHasKey('_ctxt_trueBool', $message_array);
  86. $this->assertEquals(1, $message_array['_ctxt_trueBool']);
  87. $this->assertArrayHasKey('_ctxt_falseBool', $message_array);
  88. $this->assertEquals(0, $message_array['_ctxt_falseBool']);
  89. // Test with extraPrefix
  90. $formatter = new GelfMessageFormatter(null, null, 'CTX');
  91. $message = $formatter->format($record);
  92. $this->assertInstanceOf('Gelf\Message', $message);
  93. $message_array = $message->toArray();
  94. $this->assertArrayHasKey('_CTXfrom', $message_array);
  95. $this->assertEquals('logger', $message_array['_CTXfrom']);
  96. $this->assertArrayHasKey('_CTXtrueBool', $message_array);
  97. $this->assertEquals(1, $message_array['_CTXtrueBool']);
  98. $this->assertArrayHasKey('_CTXfalseBool', $message_array);
  99. $this->assertEquals(0, $message_array['_CTXfalseBool']);
  100. }
  101. /**
  102. * @covers Monolog\Formatter\GelfMessageFormatter::format
  103. */
  104. public function testFormatWithContextContainingException()
  105. {
  106. $formatter = new GelfMessageFormatter();
  107. $record = $this->getRecord(
  108. Level::Error,
  109. 'log',
  110. channel: 'meh',
  111. context: ['from' => 'logger', 'exception' => [
  112. 'class' => '\Exception',
  113. 'file' => '/some/file/in/dir.php:56',
  114. 'trace' => ['/some/file/1.php:23', '/some/file/2.php:3'],
  115. ]],
  116. datetime: new \DateTimeImmutable("@0"),
  117. );
  118. $message = $formatter->format($record);
  119. $this->assertInstanceOf('Gelf\Message', $message);
  120. $this->assertEquals("/some/file/in/dir.php", $message->getAdditional('file'));
  121. $this->assertEquals("56", $message->getAdditional('line'));
  122. }
  123. /**
  124. * @covers Monolog\Formatter\GelfMessageFormatter::format
  125. */
  126. public function testFormatWithExtra()
  127. {
  128. $formatter = new GelfMessageFormatter();
  129. $record = $this->getRecord(
  130. Level::Error,
  131. 'log',
  132. channel: 'meh',
  133. context: ['from' => 'logger'],
  134. datetime: new \DateTimeImmutable("@0"),
  135. extra: ['key' => 'pair', 'trueBool' => true, 'falseBool' => false],
  136. );
  137. $message = $formatter->format($record);
  138. $this->assertInstanceOf('Gelf\Message', $message);
  139. $message_array = $message->toArray();
  140. $this->assertArrayHasKey('_key', $message_array);
  141. $this->assertEquals('pair', $message_array['_key']);
  142. $this->assertArrayHasKey('_trueBool', $message_array);
  143. $this->assertEquals(1, $message_array['_trueBool']);
  144. $this->assertArrayHasKey('_falseBool', $message_array);
  145. $this->assertEquals(0, $message_array['_falseBool']);
  146. // Test with extraPrefix
  147. $formatter = new GelfMessageFormatter(null, 'EXT');
  148. $message = $formatter->format($record);
  149. $this->assertInstanceOf('Gelf\Message', $message);
  150. $message_array = $message->toArray();
  151. $this->assertArrayHasKey('_EXTkey', $message_array);
  152. $this->assertEquals('pair', $message_array['_EXTkey']);
  153. $this->assertArrayHasKey('_EXTtrueBool', $message_array);
  154. $this->assertEquals(1, $message_array['_EXTtrueBool']);
  155. $this->assertArrayHasKey('_EXTfalseBool', $message_array);
  156. $this->assertEquals(0, $message_array['_EXTfalseBool']);
  157. }
  158. public function testFormatWithLargeData()
  159. {
  160. $formatter = new GelfMessageFormatter();
  161. $record = $this->getRecord(
  162. Level::Error,
  163. 'log',
  164. channel: 'meh',
  165. context: ['exception' => str_repeat(' ', 32767)],
  166. datetime: new \DateTimeImmutable("@0"),
  167. extra: ['key' => str_repeat(' ', 32767)],
  168. );
  169. $message = $formatter->format($record);
  170. $messageArray = $message->toArray();
  171. // 200 for padding + metadata
  172. $length = 200;
  173. foreach ($messageArray as $key => $value) {
  174. if (!\in_array($key, ['level', 'timestamp']) && \is_string($value)) {
  175. $length += \strlen($value);
  176. }
  177. }
  178. $this->assertLessThanOrEqual(65792, $length, 'The message length is no longer than the maximum allowed length');
  179. }
  180. public function testFormatWithUnlimitedLength()
  181. {
  182. $formatter = new GelfMessageFormatter('LONG_SYSTEM_NAME', null, 'ctxt_', PHP_INT_MAX);
  183. $record = $this->getRecord(
  184. Level::Error,
  185. 'log',
  186. channel: 'meh',
  187. context: ['exception' => str_repeat(' ', 32767 * 2)],
  188. datetime: new \DateTimeImmutable("@0"),
  189. extra: ['key' => str_repeat(' ', 32767 * 2)],
  190. );
  191. $message = $formatter->format($record);
  192. $messageArray = $message->toArray();
  193. // 200 for padding + metadata
  194. $length = 200;
  195. foreach ($messageArray as $key => $value) {
  196. if (!\in_array($key, ['level', 'timestamp'])) {
  197. $length += \strlen($value);
  198. }
  199. }
  200. $this->assertGreaterThanOrEqual(131289, $length, 'The message should not be truncated');
  201. }
  202. public function testFormatWithLargeCyrillicData()
  203. {
  204. $formatter = new GelfMessageFormatter();
  205. $record = $this->getRecord(
  206. Level::Error,
  207. str_repeat('в', 32767),
  208. channel: 'meh',
  209. context: ['exception' => str_repeat('а', 32767)],
  210. datetime: new \DateTimeImmutable("@0"),
  211. extra: ['key' => str_repeat('б', 32767)],
  212. );
  213. $message = $formatter->format($record);
  214. $messageArray = $message->toArray();
  215. $messageString = json_encode($messageArray);
  216. $this->assertIsString($messageString);
  217. }
  218. private function isLegacy()
  219. {
  220. return interface_exists('\Gelf\IMessagePublisher');
  221. }
  222. }