JsonFormatterTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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\Logger;
  12. use Monolog\Test\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('{"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));
  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 = [
  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 = [
  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. $formatter = new JsonFormatter();
  90. $throwable = new \Error('Foo');
  91. $formattedThrowable = $this->formatException($throwable);
  92. $message = $this->formatRecordWithExceptionInContext($formatter, $throwable);
  93. $this->assertContextContainsFormattedException($formattedThrowable, $message);
  94. }
  95. /**
  96. * @param string $expected
  97. * @param string $actual
  98. *
  99. * @internal param string $exception
  100. */
  101. private function assertContextContainsFormattedException($expected, $actual)
  102. {
  103. $this->assertEquals(
  104. '{"level_name":"CRITICAL","channel":"core","context":{"exception":'.$expected.'},"datetime":null,"extra":[],"message":"foobar"}'."\n",
  105. $actual
  106. );
  107. }
  108. /**
  109. * @param JsonFormatter $formatter
  110. * @param \Exception|\Throwable $exception
  111. *
  112. * @return string
  113. */
  114. private function formatRecordWithExceptionInContext(JsonFormatter $formatter, $exception)
  115. {
  116. $message = $formatter->format(array(
  117. 'level_name' => 'CRITICAL',
  118. 'channel' => 'core',
  119. 'context' => array('exception' => $exception),
  120. 'datetime' => null,
  121. 'extra' => array(),
  122. 'message' => 'foobar',
  123. ));
  124. return $message;
  125. }
  126. /**
  127. * @param \Exception|\Throwable $exception
  128. *
  129. * @return string
  130. */
  131. private function formatExceptionFilePathWithLine($exception)
  132. {
  133. $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
  134. $path = substr(json_encode($exception->getFile(), $options), 1, -1);
  135. return $path . ':' . $exception->getLine();
  136. }
  137. /**
  138. * @param \Exception|\Throwable $exception
  139. *
  140. * @param null|string $previous
  141. *
  142. * @return string
  143. */
  144. private function formatException($exception, $previous = null)
  145. {
  146. $formattedException =
  147. '{"class":"' . get_class($exception) .
  148. '","message":"' . $exception->getMessage() .
  149. '","code":' . $exception->getCode() .
  150. ',"file":"' . $this->formatExceptionFilePathWithLine($exception) .
  151. ($previous ? '","previous":' . $previous : '"') .
  152. '}';
  153. return $formattedException;
  154. }
  155. }