JsonFormatterTest.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  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\Logger;
  12. use Monolog\TestCase;
  13. class JsonFormatterTest extends TestCase
  14. {
  15. /**
  16. * @covers Monolog\Formatter\JsonFormatter::__construct
  17. * @covers Monolog\Formatter\JsonFormatter::getBatchMode
  18. * @covers Monolog\Formatter\JsonFormatter::isAppendingNewlines
  19. */
  20. public function testConstruct()
  21. {
  22. $formatter = new JsonFormatter();
  23. $this->assertEquals(JsonFormatter::BATCH_MODE_JSON, $formatter->getBatchMode());
  24. $this->assertEquals(true, $formatter->isAppendingNewlines());
  25. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES, false);
  26. $this->assertEquals(JsonFormatter::BATCH_MODE_NEWLINES, $formatter->getBatchMode());
  27. $this->assertEquals(false, $formatter->isAppendingNewlines());
  28. }
  29. /**
  30. * @covers Monolog\Formatter\JsonFormatter::format
  31. */
  32. public function testFormat()
  33. {
  34. $formatter = new JsonFormatter();
  35. $record = $this->getRecord();
  36. $this->assertEquals(json_encode($record)."\n", $formatter->format($record));
  37. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_JSON, false);
  38. $record = $this->getRecord();
  39. $this->assertEquals(json_encode($record), $formatter->format($record));
  40. }
  41. /**
  42. * @covers Monolog\Formatter\JsonFormatter::formatBatch
  43. * @covers Monolog\Formatter\JsonFormatter::formatBatchJson
  44. */
  45. public function testFormatBatch()
  46. {
  47. $formatter = new JsonFormatter();
  48. $records = array(
  49. $this->getRecord(Logger::WARNING),
  50. $this->getRecord(Logger::DEBUG),
  51. );
  52. $this->assertEquals(json_encode($records), $formatter->formatBatch($records));
  53. }
  54. /**
  55. * @covers Monolog\Formatter\JsonFormatter::formatBatch
  56. * @covers Monolog\Formatter\JsonFormatter::formatBatchNewlines
  57. */
  58. public function testFormatBatchNewlines()
  59. {
  60. $formatter = new JsonFormatter(JsonFormatter::BATCH_MODE_NEWLINES);
  61. $records = $expected = array(
  62. $this->getRecord(Logger::WARNING),
  63. $this->getRecord(Logger::DEBUG),
  64. );
  65. array_walk($expected, function (&$value, $key) {
  66. $value = json_encode($value);
  67. });
  68. $this->assertEquals(implode("\n", $expected), $formatter->formatBatch($records));
  69. }
  70. public function testDefFormatWithException()
  71. {
  72. $formatter = new JsonFormatter();
  73. $exception = new \RuntimeException('Foo');
  74. $formattedException = $this->formatException($exception);
  75. $message = $this->formatRecordWithExceptionInContext($formatter, $exception);
  76. $this->assertContextContainsFormattedException($formattedException, $message);
  77. }
  78. public function testDefFormatWithPreviousException()
  79. {
  80. $formatter = new JsonFormatter();
  81. $exception = new \RuntimeException('Foo', 0, new \LogicException('Wut?'));
  82. $formattedPrevException = $this->formatException($exception->getPrevious());
  83. $formattedException = $this->formatException($exception, $formattedPrevException);
  84. $message = $this->formatRecordWithExceptionInContext($formatter, $exception);
  85. $this->assertContextContainsFormattedException($formattedException, $message);
  86. }
  87. public function testDefFormatWithThrowable()
  88. {
  89. if (!class_exists('Error') || !is_subclass_of('Error', 'Throwable')) {
  90. $this->markTestSkipped('Requires PHP >=7');
  91. }
  92. $formatter = new JsonFormatter();
  93. $throwable = new \Error('Foo');
  94. $formattedThrowable = $this->formatException($throwable);
  95. $message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
  96. $this->assertContextContainsFormattedException($formattedThrowable, $message);
  97. }
  98. /**
  99. * @param string $expected
  100. * @param string $actual
  101. *
  102. * @internal param string $exception
  103. */
  104. private function assertContextContainsFormattedException($expected, $actual)
  105. {
  106. $this->assertEquals(
  107. '{"level_name":"CRITICAL","channel":"core","context":{"exception":'.$expected.'},"datetime":null,"extra":[],"message":"foobar"}'."\n",
  108. $actual
  109. );
  110. }
  111. /**
  112. * @param JsonFormatter $formatter
  113. * @param \Exception|\Throwable $exception
  114. *
  115. * @return string
  116. */
  117. private function formatRecordWithExceptionInContext(JsonFormatter $formatter, $exception)
  118. {
  119. $message = $formatter->format(array(
  120. 'level_name' => 'CRITICAL',
  121. 'channel' => 'core',
  122. 'context' => array('exception' => $exception),
  123. 'datetime' => null,
  124. 'extra' => array(),
  125. 'message' => 'foobar',
  126. ));
  127. return $message;
  128. }
  129. /**
  130. * @param \Exception|\Throwable $exception
  131. *
  132. * @return string
  133. */
  134. private function formatExceptionFilePathWithLine($exception)
  135. {
  136. $options = 0;
  137. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  138. $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
  139. }
  140. $path = substr(json_encode($exception->getFile(), $options), 1, -1);
  141. return $path . ':' . $exception->getLine();
  142. }
  143. /**
  144. * @param \Exception|\Throwable $exception
  145. *
  146. * @param null|string $previous
  147. *
  148. * @return string
  149. */
  150. private function formatException($exception, $previous = null)
  151. {
  152. $formattedException =
  153. '{"class":"' . get_class($exception) .
  154. '","message":"' . $exception->getMessage() .
  155. '","code":' . $exception->getCode() .
  156. ',"file":"' . $this->formatExceptionFilePathWithLine($exception) .
  157. ($previous ? '","previous":' . $previous : '"') .
  158. '}';
  159. return $formattedException;
  160. }
  161. public function testNormalizeHandleLargeArraysWithExactly1000Items()
  162. {
  163. $formatter = new NormalizerFormatter();
  164. $largeArray = range(1, 1000);
  165. $res = $formatter->format(array(
  166. 'level_name' => 'CRITICAL',
  167. 'channel' => 'test',
  168. 'message' => 'bar',
  169. 'context' => array($largeArray),
  170. 'datetime' => new \DateTime,
  171. 'extra' => array(),
  172. ));
  173. $this->assertCount(1000, $res['context'][0]);
  174. $this->assertArrayNotHasKey('...', $res['context'][0]);
  175. }
  176. public function testNormalizeHandleLargeArrays()
  177. {
  178. $formatter = new NormalizerFormatter();
  179. $largeArray = range(1, 2000);
  180. $res = $formatter->format(array(
  181. 'level_name' => 'CRITICAL',
  182. 'channel' => 'test',
  183. 'message' => 'bar',
  184. 'context' => array($largeArray),
  185. 'datetime' => new \DateTime,
  186. 'extra' => array(),
  187. ));
  188. $this->assertCount(1001, $res['context'][0]);
  189. $this->assertEquals('Over 1000 items (2000 total), aborting normalization', $res['context'][0]['...']);
  190. }
  191. }