NormalizerFormatter.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 Exception;
  12. /**
  13. * Normalizes incoming records to remove objects/resources so it's easier to dump to various targets
  14. *
  15. * @author Jordi Boggiano <j.boggiano@seld.be>
  16. */
  17. class NormalizerFormatter implements FormatterInterface
  18. {
  19. const SIMPLE_DATE = "Y-m-d H:i:s";
  20. protected $dateFormat;
  21. /**
  22. * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
  23. */
  24. public function __construct($dateFormat = null)
  25. {
  26. $this->dateFormat = $dateFormat ?: static::SIMPLE_DATE;
  27. if (!function_exists('json_encode')) {
  28. throw new \RuntimeException('PHP\'s json extension is required to use Monolog\'s NormalizerFormatter');
  29. }
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function format(array $record)
  35. {
  36. return $this->normalize($record);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function formatBatch(array $records)
  42. {
  43. foreach ($records as $key => $record) {
  44. $records[$key] = $this->format($record);
  45. }
  46. return $records;
  47. }
  48. protected function normalize($data)
  49. {
  50. if (null === $data || is_scalar($data)) {
  51. if (is_float($data)) {
  52. if (is_infinite($data)) {
  53. return ($data > 0 ? '' : '-') . 'INF';
  54. }
  55. if (is_nan($data)) {
  56. return 'NaN';
  57. }
  58. }
  59. return $data;
  60. }
  61. if (is_array($data) || $data instanceof \Traversable) {
  62. $normalized = array();
  63. $count = 1;
  64. foreach ($data as $key => $value) {
  65. if ($count++ >= 1000) {
  66. $normalized['...'] = 'Over 1000 items, aborting normalization';
  67. break;
  68. }
  69. $normalized[$key] = $this->normalize($value);
  70. }
  71. return $normalized;
  72. }
  73. if ($data instanceof \DateTime) {
  74. return $data->format($this->dateFormat);
  75. }
  76. if (is_object($data)) {
  77. if ($data instanceof Exception) {
  78. return $this->normalizeException($data);
  79. }
  80. if (method_exists($data, '__toString')) {
  81. return sprintf("[object] (%s: %s)", get_class($data), $data);
  82. } else {
  83. return sprintf("[object] (%s: %s)", get_class($data), $this->toJson($data, true));
  84. }
  85. }
  86. if (is_resource($data)) {
  87. return '[resource]';
  88. }
  89. return '[unknown('.gettype($data).')]';
  90. }
  91. protected function normalizeException(Exception $e)
  92. {
  93. $data = array(
  94. 'class' => get_class($e),
  95. 'message' => $e->getMessage(),
  96. 'code' => $e->getCode(),
  97. 'file' => $e->getFile().':'.$e->getLine(),
  98. );
  99. $trace = $e->getTrace();
  100. foreach ($trace as $frame) {
  101. if (isset($frame['file'])) {
  102. $data['trace'][] = $frame['file'].':'.$frame['line'];
  103. } else {
  104. // We should again normalize the frames, because it might contain invalid items
  105. $data['trace'][] = $this->toJson($this->normalize($frame), true);
  106. }
  107. }
  108. if ($previous = $e->getPrevious()) {
  109. $data['previous'] = $this->normalizeException($previous);
  110. }
  111. return $data;
  112. }
  113. protected function toJson($data, $ignoreErrors = false)
  114. {
  115. // suppress json_encode errors since it's twitchy with some inputs
  116. if ($ignoreErrors) {
  117. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  118. return @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  119. }
  120. return @json_encode($data);
  121. }
  122. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  123. return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  124. }
  125. return json_encode($data);
  126. }
  127. }