NormalizerFormatter.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 Throwable;
  12. use Monolog\DateTimeImmutable;
  13. /**
  14. * Normalizes incoming records to remove objects/resources so it's easier to dump to various targets
  15. *
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. class NormalizerFormatter implements FormatterInterface
  19. {
  20. const SIMPLE_DATE = "Y-m-d\TH:i:sP";
  21. protected $dateFormat;
  22. /**
  23. * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
  24. */
  25. public function __construct(string $dateFormat = null)
  26. {
  27. $this->dateFormat = $dateFormat;
  28. if (!function_exists('json_encode')) {
  29. throw new \RuntimeException('PHP\'s json extension is required to use Monolog\'s NormalizerFormatter');
  30. }
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function format(array $record)
  36. {
  37. return $this->normalize($record);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function formatBatch(array $records)
  43. {
  44. foreach ($records as $key => $record) {
  45. $records[$key] = $this->format($record);
  46. }
  47. return $records;
  48. }
  49. protected function normalize($data)
  50. {
  51. if (null === $data || is_scalar($data)) {
  52. if (is_float($data)) {
  53. if (is_infinite($data)) {
  54. return ($data > 0 ? '' : '-') . 'INF';
  55. }
  56. if (is_nan($data)) {
  57. return 'NaN';
  58. }
  59. }
  60. return $data;
  61. }
  62. if (is_array($data) || $data instanceof \Traversable) {
  63. $normalized = array();
  64. $count = 1;
  65. foreach ($data as $key => $value) {
  66. if ($count++ >= 1000) {
  67. $normalized['...'] = 'Over 1000 items, aborting normalization';
  68. break;
  69. }
  70. $normalized[$key] = $this->normalize($value);
  71. }
  72. return $normalized;
  73. }
  74. if ($data instanceof \DateTimeInterface) {
  75. if ($data instanceof DateTimeImmutable) {
  76. return (string) $data;
  77. }
  78. return $data->format($this->dateFormat ?: static::SIMPLE_DATE);
  79. }
  80. if (is_object($data)) {
  81. if ($data instanceof Throwable) {
  82. return $this->normalizeException($data);
  83. }
  84. if ($data instanceof \JsonSerializable) {
  85. $value = $data->jsonSerialize();
  86. } elseif (method_exists($data, '__toString')) {
  87. $value = $data->__toString();
  88. } else {
  89. // the rest is normalized by json encoding and decoding it
  90. $encoded = $this->toJson($data, true);
  91. if ($encoded === false) {
  92. $value = 'JSON_ERROR';
  93. } else {
  94. $value = json_decode($encoded, true);
  95. }
  96. }
  97. return [get_class($data) => $value];
  98. }
  99. if (is_resource($data)) {
  100. return sprintf('[resource(%s)]', get_resource_type($data));
  101. }
  102. return '[unknown('.gettype($data).')]';
  103. }
  104. protected function normalizeException(Throwable $e)
  105. {
  106. $data = array(
  107. 'class' => get_class($e),
  108. 'message' => $e->getMessage(),
  109. 'code' => $e->getCode(),
  110. 'file' => $e->getFile().':'.$e->getLine(),
  111. );
  112. $trace = $e->getTrace();
  113. foreach ($trace as $frame) {
  114. if (isset($frame['file'])) {
  115. $data['trace'][] = $frame['file'].':'.$frame['line'];
  116. } elseif (isset($frame['function']) && $frame['function'] === '{closure}') {
  117. // We should again normalize the frames, because it might contain invalid items
  118. $data['trace'][] = $frame['function'];
  119. } else {
  120. // We should again normalize the frames, because it might contain invalid items
  121. $data['trace'][] = $this->toJson($this->normalize($frame), true);
  122. }
  123. }
  124. if ($previous = $e->getPrevious()) {
  125. $data['previous'] = $this->normalizeException($previous);
  126. }
  127. return $data;
  128. }
  129. /**
  130. * Return the JSON representation of a value
  131. *
  132. * @param mixed $data
  133. * @param bool $ignoreErrors
  134. * @throws \RuntimeException if encoding fails and errors are not ignored
  135. * @return string|bool
  136. */
  137. protected function toJson($data, $ignoreErrors = false)
  138. {
  139. // suppress json_encode errors since it's twitchy with some inputs
  140. if ($ignoreErrors) {
  141. return @$this->jsonEncode($data);
  142. }
  143. $json = $this->jsonEncode($data);
  144. if ($json === false) {
  145. $json = $this->handleJsonError(json_last_error(), $data);
  146. }
  147. return $json;
  148. }
  149. /**
  150. * @param mixed $data
  151. * @return string|bool JSON encoded data or false on failure
  152. */
  153. private function jsonEncode($data)
  154. {
  155. return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  156. }
  157. /**
  158. * Handle a json_encode failure.
  159. *
  160. * If the failure is due to invalid string encoding, try to clean the
  161. * input and encode again. If the second encoding iattempt fails, the
  162. * inital error is not encoding related or the input can't be cleaned then
  163. * raise a descriptive exception.
  164. *
  165. * @param int $code return code of json_last_error function
  166. * @param mixed $data data that was meant to be encoded
  167. * @throws \RuntimeException if failure can't be corrected
  168. * @return string JSON encoded data after error correction
  169. */
  170. private function handleJsonError(int $code, $data): string
  171. {
  172. if ($code !== JSON_ERROR_UTF8) {
  173. $this->throwEncodeError($code, $data);
  174. }
  175. if (is_string($data)) {
  176. $this->detectAndCleanUtf8($data);
  177. } elseif (is_array($data)) {
  178. array_walk_recursive($data, array($this, 'detectAndCleanUtf8'));
  179. } else {
  180. $this->throwEncodeError($code, $data);
  181. }
  182. $json = $this->jsonEncode($data);
  183. if ($json === false) {
  184. $this->throwEncodeError(json_last_error(), $data);
  185. }
  186. return $json;
  187. }
  188. /**
  189. * Throws an exception according to a given code with a customized message
  190. *
  191. * @param int $code return code of json_last_error function
  192. * @param mixed $data data that was meant to be encoded
  193. * @throws \RuntimeException
  194. */
  195. private function throwEncodeError(int $code, $data)
  196. {
  197. switch ($code) {
  198. case JSON_ERROR_DEPTH:
  199. $msg = 'Maximum stack depth exceeded';
  200. break;
  201. case JSON_ERROR_STATE_MISMATCH:
  202. $msg = 'Underflow or the modes mismatch';
  203. break;
  204. case JSON_ERROR_CTRL_CHAR:
  205. $msg = 'Unexpected control character found';
  206. break;
  207. case JSON_ERROR_UTF8:
  208. $msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
  209. break;
  210. default:
  211. $msg = 'Unknown error';
  212. }
  213. throw new \RuntimeException('JSON encoding failed: '.$msg.'. Encoding: '.var_export($data, true));
  214. }
  215. /**
  216. * Detect invalid UTF-8 string characters and convert to valid UTF-8.
  217. *
  218. * Valid UTF-8 input will be left unmodified, but strings containing
  219. * invalid UTF-8 codepoints will be reencoded as UTF-8 with an assumed
  220. * original encoding of ISO-8859-15. This conversion may result in
  221. * incorrect output if the actual encoding was not ISO-8859-15, but it
  222. * will be clean UTF-8 output and will not rely on expensive and fragile
  223. * detection algorithms.
  224. *
  225. * Function converts the input in place in the passed variable so that it
  226. * can be used as a callback for array_walk_recursive.
  227. *
  228. * @param mixed &$data Input to check and convert if needed
  229. * @private
  230. */
  231. public function detectAndCleanUtf8(&$data)
  232. {
  233. if (is_string($data) && !preg_match('//u', $data)) {
  234. $data = preg_replace_callback(
  235. '/[\x80-\xFF]+/',
  236. function ($m) { return utf8_encode($m[0]); },
  237. $data
  238. );
  239. $data = str_replace(
  240. array('¤', '¦', '¨', '´', '¸', '¼', '½', '¾'),
  241. array('€', 'Š', 'š', 'Ž', 'ž', 'Œ', 'œ', 'Ÿ'),
  242. $data
  243. );
  244. }
  245. }
  246. }