JsonFormatterTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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\LogRecord;
  13. use JsonSerializable;
  14. use Monolog\Test\TestCase;
  15. class JsonFormatterTest extends TestCase
  16. {
  17. /**
  18. * @covers Monolog\Formatter\JsonFormatter::__construct
  19. * @covers Monolog\Formatter\JsonFormatter::getBatchMode
  20. * @covers Monolog\Formatter\JsonFormatter::isAppendingNewlines
  21. */
  22. public function testConstruct()
  23. {
  24. $formatter = new JsonFormatter();
  25. $this->assertEquals(JsonFormatter::BATCH_MODE_JSON, $formatter->getBatchMode());
  26. $this->assertEquals(true, $formatter->isAppendingNewlines());
  27. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES, false);
  28. $this->assertEquals(JsonFormatter::BATCH_MODE_NEWLINES, $formatter->getBatchMode());
  29. $this->assertEquals(false, $formatter->isAppendingNewlines());
  30. }
  31. /**
  32. * @covers Monolog\Formatter\JsonFormatter::format
  33. */
  34. public function testFormat()
  35. {
  36. $formatter = new JsonFormatter();
  37. $record = $this->getRecord();
  38. $this->assertEquals(json_encode($record->toArray(), JSON_FORCE_OBJECT)."\n", $formatter->format($record));
  39. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
  40. $record = $this->getRecord();
  41. $this->assertEquals('{"message":"test","context":{},"level":300,"level_name":"WARNING","channel":"test","datetime":"'.$record->datetime->format('Y-m-d\TH:i:s.uP').'","extra":{}}', $formatter->format($record));
  42. }
  43. /**
  44. * @covers Monolog\Formatter\JsonFormatter::format
  45. */
  46. public function testFormatWithPrettyPrint()
  47. {
  48. $formatter = new JsonFormatter();
  49. $formatter->setJsonPrettyPrint(true);
  50. $record = $this->getRecord();
  51. $this->assertEquals(json_encode($record->toArray(), JSON_PRETTY_PRINT | JSON_FORCE_OBJECT)."\n", $formatter->format($record));
  52. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
  53. $formatter->setJsonPrettyPrint(true);
  54. $record = $this->getRecord();
  55. $this->assertEquals(
  56. '{
  57. "message": "test",
  58. "context": {},
  59. "level": 300,
  60. "level_name": "WARNING",
  61. "channel": "test",
  62. "datetime": "'.$record->datetime->format('Y-m-d\TH:i:s.uP').'",
  63. "extra": {}
  64. }',
  65. $formatter->format($record)
  66. );
  67. $formatter->setJsonPrettyPrint(false);
  68. $record = $this->getRecord();
  69. $this->assertEquals('{"message":"test","context":{},"level":300,"level_name":"WARNING","channel":"test","datetime":"'.$record->datetime->format('Y-m-d\TH:i:s.uP').'","extra":{}}', $formatter->format($record));
  70. }
  71. /**
  72. * @covers Monolog\Formatter\JsonFormatter::formatBatch
  73. * @covers Monolog\Formatter\JsonFormatter::formatBatchJson
  74. */
  75. public function testFormatBatch()
  76. {
  77. $formatter = new JsonFormatter();
  78. $records = [
  79. $this->getRecord(Level::Warning),
  80. $this->getRecord(Level::Debug),
  81. ];
  82. $this->assertEquals(json_encode($records), $formatter->formatBatch($records));
  83. }
  84. /**
  85. * @covers Monolog\Formatter\JsonFormatter::formatBatch
  86. * @covers Monolog\Formatter\JsonFormatter::formatBatchNewlines
  87. */
  88. public function testFormatBatchNewlines()
  89. {
  90. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES);
  91. $records = [
  92. $this->getRecord(Level::Warning),
  93. $this->getRecord(Level::Debug),
  94. ];
  95. $expected = array_map(fn (LogRecord $record) => json_encode($record->toArray(), JSON_FORCE_OBJECT), $records);
  96. $this->assertEquals(implode("\n", $expected), $formatter->formatBatch($records));
  97. }
  98. public function testDefFormatWithException()
  99. {
  100. $formatter = new JsonFormatter();
  101. $exception = new \RuntimeException('Foo');
  102. $formattedException = $this->formatException($exception);
  103. $message = $this->formatRecordWithExceptionInContext($formatter, $exception);
  104. $this->assertContextContainsFormattedException($formattedException, $message);
  105. }
  106. public function testDefFormatWithPreviousException()
  107. {
  108. $formatter = new JsonFormatter();
  109. $exception = new \RuntimeException('Foo', 0, new \LogicException('Wut?'));
  110. $formattedPrevException = $this->formatException($exception->getPrevious());
  111. $formattedException = $this->formatException($exception, $formattedPrevException);
  112. $message = $this->formatRecordWithExceptionInContext($formatter, $exception);
  113. $this->assertContextContainsFormattedException($formattedException, $message);
  114. }
  115. public function testDefFormatWithThrowable()
  116. {
  117. $formatter = new JsonFormatter();
  118. $throwable = new \Error('Foo');
  119. $formattedThrowable = $this->formatException($throwable);
  120. $message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
  121. $this->assertContextContainsFormattedException($formattedThrowable, $message);
  122. }
  123. public function testMaxNormalizeDepth()
  124. {
  125. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, true);
  126. $formatter->setMaxNormalizeDepth(1);
  127. $throwable = new \Error('Foo');
  128. $message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
  129. $this->assertContextContainsFormattedException('"Over 1 levels deep, aborting normalization"', $message);
  130. }
  131. public function testMaxNormalizeItemCountWith0ItemsMax()
  132. {
  133. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, true);
  134. $formatter->setMaxNormalizeDepth(9);
  135. $formatter->setMaxNormalizeItemCount(0);
  136. $throwable = new \Error('Foo');
  137. $message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
  138. $this->assertEquals(
  139. '{"...":"Over 0 items (7 total), aborting normalization"}'."\n",
  140. $message
  141. );
  142. }
  143. public function testMaxNormalizeItemCountWith2ItemsMax()
  144. {
  145. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, true);
  146. $formatter->setMaxNormalizeDepth(9);
  147. $formatter->setMaxNormalizeItemCount(2);
  148. $throwable = new \Error('Foo');
  149. $message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
  150. $this->assertEquals(
  151. '{"message":"foobar","context":{"exception":{"class":"Error","message":"Foo","code":0,"file":"'.__FILE__.':'.(__LINE__ - 5).'"}},"...":"Over 2 items (7 total), aborting normalization"}'."\n",
  152. $message
  153. );
  154. }
  155. public function testDefFormatWithResource()
  156. {
  157. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
  158. $record = $this->getRecord(
  159. context: ['field_resource' => opendir(__DIR__)],
  160. );
  161. $this->assertEquals('{"message":"test","context":{"field_resource":"[resource(stream)]"},"level":300,"level_name":"WARNING","channel":"test","datetime":"'.$record->datetime->format('Y-m-d\TH:i:s.uP').'","extra":{}}', $formatter->format($record));
  162. }
  163. /**
  164. * @internal param string $exception
  165. */
  166. private function assertContextContainsFormattedException(string $expected, string $actual)
  167. {
  168. $this->assertEquals(
  169. '{"message":"foobar","context":{"exception":'.$expected.'},"level":500,"level_name":"CRITICAL","channel":"core","datetime":"2022-02-22T00:00:00+00:00","extra":{}}'."\n",
  170. $actual
  171. );
  172. }
  173. private function formatRecordWithExceptionInContext(JsonFormatter $formatter, \Throwable $exception): string
  174. {
  175. $message = $formatter->format($this->getRecord(
  176. Level::Critical,
  177. 'foobar',
  178. channel: 'core',
  179. context: ['exception' => $exception],
  180. datetime: new \DateTimeImmutable('2022-02-22 00:00:00'),
  181. ));
  182. return $message;
  183. }
  184. /**
  185. * @param \Exception|\Throwable $exception
  186. */
  187. private function formatExceptionFilePathWithLine($exception): string
  188. {
  189. $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
  190. $path = substr(json_encode($exception->getFile(), $options), 1, -1);
  191. return $path . ':' . $exception->getLine();
  192. }
  193. /**
  194. * @param \Exception|\Throwable $exception
  195. */
  196. private function formatException($exception, ?string $previous = null): string
  197. {
  198. $formattedException =
  199. '{"class":"' . get_class($exception) .
  200. '","message":"' . $exception->getMessage() .
  201. '","code":' . $exception->getCode() .
  202. ',"file":"' . $this->formatExceptionFilePathWithLine($exception) .
  203. ($previous ? '","previous":' . $previous : '"') .
  204. '}';
  205. return $formattedException;
  206. }
  207. public function testNormalizeHandleLargeArraysWithExactly1000Items()
  208. {
  209. $formatter = new NormalizerFormatter();
  210. $largeArray = range(1, 1000);
  211. $res = $formatter->format($this->getRecord(
  212. Level::Critical,
  213. 'bar',
  214. channel: 'test',
  215. context: [$largeArray],
  216. ));
  217. $this->assertCount(1000, $res['context'][0]);
  218. $this->assertArrayNotHasKey('...', $res['context'][0]);
  219. }
  220. public function testNormalizeHandleLargeArrays()
  221. {
  222. $formatter = new NormalizerFormatter();
  223. $largeArray = range(1, 2000);
  224. $res = $formatter->format($this->getRecord(
  225. Level::Critical,
  226. 'bar',
  227. channel: 'test',
  228. context: [$largeArray],
  229. ));
  230. $this->assertCount(1001, $res['context'][0]);
  231. $this->assertEquals('Over 1000 items (2000 total), aborting normalization', $res['context'][0]['...']);
  232. }
  233. public function testEmptyContextAndExtraFieldsCanBeIgnored()
  234. {
  235. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, true, true);
  236. $record = $formatter->format($this->getRecord(
  237. Level::Debug,
  238. 'Testing',
  239. channel: 'test',
  240. datetime: new \DateTimeImmutable('2022-02-22 00:00:00'),
  241. ));
  242. $this->assertSame(
  243. '{"message":"Testing","level":100,"level_name":"DEBUG","channel":"test","datetime":"2022-02-22T00:00:00+00:00"}'."\n",
  244. $record
  245. );
  246. }
  247. public function testFormatObjects()
  248. {
  249. $formatter = new JsonFormatter();
  250. $record = $formatter->format($this->getRecord(
  251. Level::Debug,
  252. 'Testing',
  253. channel: 'test',
  254. datetime: new \DateTimeImmutable('2022-02-22 00:00:00'),
  255. context: [
  256. 'public' => new TestJsonNormPublic,
  257. 'private' => new TestJsonNormPrivate,
  258. 'withToStringAndJson' => new TestJsonNormWithToStringAndJson,
  259. 'withToString' => new TestJsonNormWithToString,
  260. ],
  261. ));
  262. $this->assertSame(
  263. '{"message":"Testing","context":{"public":{"foo":"fooValue"},"private":{},"withToStringAndJson":["json serialized"],"withToString":"stringified"},"level":100,"level_name":"DEBUG","channel":"test","datetime":"2022-02-22T00:00:00+00:00","extra":{}}'."\n",
  264. $record
  265. );
  266. }
  267. }
  268. class TestJsonNormPublic
  269. {
  270. public $foo = 'fooValue';
  271. }
  272. class TestJsonNormPrivate
  273. {
  274. private $foo = 'fooValue';
  275. }
  276. class TestJsonNormWithToStringAndJson implements JsonSerializable
  277. {
  278. public function jsonSerialize(): mixed
  279. {
  280. return ['json serialized'];
  281. }
  282. public function __toString()
  283. {
  284. return 'SHOULD NOT SHOW UP';
  285. }
  286. }
  287. class TestJsonNormWithToString
  288. {
  289. public function __toString()
  290. {
  291. return 'stringified';
  292. }
  293. }