NormalizerFormatter.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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. /**
  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 \DateTimeInterface) {
  74. return $data->format($this->dateFormat);
  75. }
  76. if (is_object($data)) {
  77. if ($data instanceof Throwable) {
  78. return $this->normalizeException($data);
  79. }
  80. // non-serializable objects that implement __toString stringified
  81. if (method_exists($data, '__toString') && !$data instanceof \JsonSerializable) {
  82. $value = $data->__toString();
  83. } else {
  84. // the rest is json-serialized in some way
  85. $value = $this->toJson($data, true);
  86. }
  87. return sprintf("[object] (%s: %s)", get_class($data), $value);
  88. }
  89. if (is_resource($data)) {
  90. return sprintf('[resource] (%s)', get_resource_type($data));
  91. }
  92. return '[unknown('.gettype($data).')]';
  93. }
  94. protected function normalizeException($e)
  95. {
  96. if (!$e instanceof Throwable) {
  97. throw new \InvalidArgumentException('Throwable expected, got '.gettype($e).' / '.get_class($e));
  98. }
  99. $data = array(
  100. 'class' => get_class($e),
  101. 'message' => $e->getMessage(),
  102. 'code' => $e->getCode(),
  103. 'file' => $e->getFile().':'.$e->getLine(),
  104. );
  105. $trace = $e->getTrace();
  106. foreach ($trace as $frame) {
  107. if (isset($frame['file'])) {
  108. $data['trace'][] = $frame['file'].':'.$frame['line'];
  109. } elseif (isset($frame['function']) && $frame['function'] === '{closure}') {
  110. // We should again normalize the frames, because it might contain invalid items
  111. $data['trace'][] = $frame['function'];
  112. } else {
  113. // We should again normalize the frames, because it might contain invalid items
  114. $data['trace'][] = $this->toJson($this->normalize($frame), true);
  115. }
  116. }
  117. if ($previous = $e->getPrevious()) {
  118. $data['previous'] = $this->normalizeException($previous);
  119. }
  120. return $data;
  121. }
  122. /**
  123. * Return the JSON representation of a value
  124. *
  125. * @param mixed $data
  126. * @param bool $ignoreErrors
  127. * @throws \RuntimeException if encoding fails and errors are not ignored
  128. * @return string
  129. */
  130. protected function toJson($data, $ignoreErrors = false)
  131. {
  132. // suppress json_encode errors since it's twitchy with some inputs
  133. if ($ignoreErrors) {
  134. return @$this->jsonEncode($data);
  135. }
  136. $json = $this->jsonEncode($data);
  137. if ($json === false) {
  138. $json = $this->handleJsonError(json_last_error(), $data);
  139. }
  140. return $json;
  141. }
  142. /**
  143. * @param mixed $data
  144. * @return string JSON encoded data or null on failure
  145. */
  146. private function jsonEncode($data)
  147. {
  148. return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
  149. }
  150. /**
  151. * Handle a json_encode failure.
  152. *
  153. * If the failure is due to invalid string encoding, try to clean the
  154. * input and encode again. If the second encoding iattempt fails, the
  155. * inital error is not encoding related or the input can't be cleaned then
  156. * raise a descriptive exception.
  157. *
  158. * @param int $code return code of json_last_error function
  159. * @param mixed $data data that was meant to be encoded
  160. * @throws \RuntimeException if failure can't be corrected
  161. * @return string JSON encoded data after error correction
  162. */
  163. private function handleJsonError($code, $data)
  164. {
  165. if ($code !== JSON_ERROR_UTF8) {
  166. $this->throwEncodeError($code, $data);
  167. }
  168. if (is_string($data)) {
  169. $this->detectAndCleanUtf8($data);
  170. } elseif (is_array($data)) {
  171. array_walk_recursive($data, array($this, 'detectAndCleanUtf8'));
  172. } else {
  173. $this->throwEncodeError($code, $data);
  174. }
  175. $json = $this->jsonEncode($data);
  176. if ($json === false) {
  177. $this->throwEncodeError(json_last_error(), $data);
  178. }
  179. return $json;
  180. }
  181. /**
  182. * Throws an exception according to a given code with a customized message
  183. *
  184. * @param int $code return code of json_last_error function
  185. * @param mixed $data data that was meant to be encoded
  186. * @throws \RuntimeException
  187. */
  188. private function throwEncodeError($code, $data)
  189. {
  190. switch ($code) {
  191. case JSON_ERROR_DEPTH:
  192. $msg = 'Maximum stack depth exceeded';
  193. break;
  194. case JSON_ERROR_STATE_MISMATCH:
  195. $msg = 'Underflow or the modes mismatch';
  196. break;
  197. case JSON_ERROR_CTRL_CHAR:
  198. $msg = 'Unexpected control character found';
  199. break;
  200. case JSON_ERROR_UTF8:
  201. $msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
  202. break;
  203. default:
  204. $msg = 'Unknown error';
  205. }
  206. throw new \RuntimeException('JSON encoding failed: '.$msg.'. Encoding: '.var_export($data, true));
  207. }
  208. /**
  209. * Detect invalid UTF-8 string characters and convert to valid UTF-8.
  210. *
  211. * Valid UTF-8 input will be left unmodified, but strings containing
  212. * invalid UTF-8 codepoints will be reencoded as UTF-8 with an assumed
  213. * original encoding of ISO-8859-15. This conversion may result in
  214. * incorrect output if the actual encoding was not ISO-8859-15, but it
  215. * will be clean UTF-8 output and will not rely on expensive and fragile
  216. * detection algorithms.
  217. *
  218. * Function converts the input in place in the passed variable so that it
  219. * can be used as a callback for array_walk_recursive.
  220. *
  221. * @param mixed &$data Input to check and convert if needed
  222. * @private
  223. */
  224. public function detectAndCleanUtf8(&$data)
  225. {
  226. if (is_string($data) && !preg_match('//u', $data)) {
  227. $data = preg_replace_callback(
  228. '/[\x80-\xFF]+/',
  229. function ($m) { return utf8_encode($m[0]); },
  230. $data
  231. );
  232. $data = str_replace(
  233. array('¤', '¦', '¨', '´', '¸', '¼', '½', '¾'),
  234. array('€', 'Š', 'š', 'Ž', 'ž', 'Œ', 'œ', 'Ÿ'),
  235. $data
  236. );
  237. }
  238. }
  239. }