mirror of
https://github.com/Seldaek/monolog.git
synced 2025-10-22 09:06:10 +02:00
Merge branch '1.x'
This commit is contained in:
@@ -28,7 +28,7 @@ class NormalizerFormatter implements FormatterInterface
|
||||
protected $maxNormalizeDepth = 9;
|
||||
protected $maxNormalizeItemCount = 1000;
|
||||
|
||||
private $jsonEncodeOptions = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION;
|
||||
private $jsonEncodeOptions = Utils::DEFAULT_JSON_FLAGS;
|
||||
|
||||
/**
|
||||
* @param string|null $dateFormat The format of the timestamp: one supported by DateTime::format
|
||||
@@ -189,7 +189,7 @@ class NormalizerFormatter implements FormatterInterface
|
||||
$data = [
|
||||
'class' => Utils::getClass($e),
|
||||
'message' => $e->getMessage(),
|
||||
'code' => $e->getCode(),
|
||||
'code' => (int) $e->getCode(),
|
||||
'file' => $e->getFile().':'.$e->getLine(),
|
||||
];
|
||||
|
||||
@@ -202,8 +202,8 @@ class NormalizerFormatter implements FormatterInterface
|
||||
$data['faultactor'] = $e->faultactor;
|
||||
}
|
||||
|
||||
if (isset($e->detail)) {
|
||||
$data['detail'] = $e->detail;
|
||||
if (isset($e->detail) && (is_string($e->detail) || is_object($e->detail) || is_array($e->detail))) {
|
||||
$data['detail'] = is_string($e->detail) ? $e->detail : reset($e->detail);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -226,130 +226,11 @@ class NormalizerFormatter implements FormatterInterface
|
||||
*
|
||||
* @param mixed $data
|
||||
* @throws \RuntimeException if encoding fails and errors are not ignored
|
||||
* @return string|bool
|
||||
* @return string if encoding fails and ignoreErrors is true 'null' is returned
|
||||
*/
|
||||
protected function toJson($data, bool $ignoreErrors = false)
|
||||
protected function toJson($data, bool $ignoreErrors = false): string
|
||||
{
|
||||
// suppress json_encode errors since it's twitchy with some inputs
|
||||
if ($ignoreErrors) {
|
||||
return @$this->jsonEncode($data);
|
||||
}
|
||||
|
||||
$json = $this->jsonEncode($data);
|
||||
|
||||
if ($json === false) {
|
||||
$json = $this->handleJsonError(json_last_error(), $data);
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $data
|
||||
* @return string|bool JSON encoded data or false on failure
|
||||
*/
|
||||
private function jsonEncode($data)
|
||||
{
|
||||
return json_encode($data, $this->jsonEncodeOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a json_encode failure.
|
||||
*
|
||||
* If the failure is due to invalid string encoding, try to clean the
|
||||
* input and encode again. If the second encoding attempt fails, the
|
||||
* initial error is not encoding related or the input can't be cleaned then
|
||||
* raise a descriptive exception.
|
||||
*
|
||||
* @param int $code return code of json_last_error function
|
||||
* @param mixed $data data that was meant to be encoded
|
||||
* @throws \RuntimeException if failure can't be corrected
|
||||
* @return string JSON encoded data after error correction
|
||||
*/
|
||||
private function handleJsonError(int $code, $data): string
|
||||
{
|
||||
if ($code !== JSON_ERROR_UTF8) {
|
||||
$this->throwEncodeError($code, $data);
|
||||
}
|
||||
|
||||
if (is_string($data)) {
|
||||
$this->detectAndCleanUtf8($data);
|
||||
} elseif (is_array($data)) {
|
||||
array_walk_recursive($data, [$this, 'detectAndCleanUtf8']);
|
||||
} else {
|
||||
$this->throwEncodeError($code, $data);
|
||||
}
|
||||
|
||||
$json = $this->jsonEncode($data);
|
||||
|
||||
if ($json === false) {
|
||||
$this->throwEncodeError(json_last_error(), $data);
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an exception according to a given code with a customized message
|
||||
*
|
||||
* @param int $code return code of json_last_error function
|
||||
* @param mixed $data data that was meant to be encoded
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
private function throwEncodeError(int $code, $data)
|
||||
{
|
||||
switch ($code) {
|
||||
case JSON_ERROR_DEPTH:
|
||||
$msg = 'Maximum stack depth exceeded';
|
||||
break;
|
||||
case JSON_ERROR_STATE_MISMATCH:
|
||||
$msg = 'Underflow or the modes mismatch';
|
||||
break;
|
||||
case JSON_ERROR_CTRL_CHAR:
|
||||
$msg = 'Unexpected control character found';
|
||||
break;
|
||||
case JSON_ERROR_UTF8:
|
||||
$msg = 'Malformed UTF-8 characters, possibly incorrectly encoded';
|
||||
break;
|
||||
default:
|
||||
$msg = 'Unknown error';
|
||||
}
|
||||
|
||||
throw new \RuntimeException('JSON encoding failed: '.$msg.'. Encoding: '.var_export($data, true));
|
||||
}
|
||||
|
||||
/**
|
||||
* Detect invalid UTF-8 string characters and convert to valid UTF-8.
|
||||
*
|
||||
* Valid UTF-8 input will be left unmodified, but strings containing
|
||||
* invalid UTF-8 codepoints will be reencoded as UTF-8 with an assumed
|
||||
* original encoding of ISO-8859-15. This conversion may result in
|
||||
* incorrect output if the actual encoding was not ISO-8859-15, but it
|
||||
* will be clean UTF-8 output and will not rely on expensive and fragile
|
||||
* detection algorithms.
|
||||
*
|
||||
* Function converts the input in place in the passed variable so that it
|
||||
* can be used as a callback for array_walk_recursive.
|
||||
*
|
||||
* @param mixed &$data Input to check and convert if needed
|
||||
* @private
|
||||
*/
|
||||
public function detectAndCleanUtf8(&$data)
|
||||
{
|
||||
if (is_string($data) && !preg_match('//u', $data)) {
|
||||
$data = preg_replace_callback(
|
||||
'/[\x80-\xFF]+/',
|
||||
function ($m) {
|
||||
return utf8_encode($m[0]);
|
||||
},
|
||||
$data
|
||||
);
|
||||
$data = str_replace(
|
||||
['¤', '¦', '¨', '´', '¸', '¼', '½', '¾'],
|
||||
['€', 'Š', 'š', 'Ž', 'ž', 'Œ', 'œ', 'Ÿ'],
|
||||
$data
|
||||
);
|
||||
}
|
||||
return Utils::jsonEncode($data, $this->jsonEncodeOptions, $ignoreErrors);
|
||||
}
|
||||
|
||||
protected function formatDate(\DateTimeInterface $date)
|
||||
|
Reference in New Issue
Block a user