32 $this->dateFormat =
$dateFormat ?: static::SIMPLE_DATE;
33 if (!function_exists(
'json_encode')) {
34 throw new \RuntimeException(
'PHP\'s json extension is required to use Monolog\'s NormalizerFormatter');
41 public function format(array $record)
51 foreach ($records as $key => $record) {
52 $records[$key] = $this->
format($record);
61 if (is_float(
$data)) {
62 if (is_infinite(
$data)) {
63 return (
$data > 0 ?
'' :
'-') .
'INF';
73 if (is_array(
$data) ||
$data instanceof \Traversable) {
74 $normalized = array();
77 foreach (
$data as $key => $value) {
78 if ($count++ >= 1000) {
79 $normalized[
'...'] =
'Over 1000 items, aborting normalization';
82 $normalized[$key] = $this->
normalize($value);
88 if (
$data instanceof \DateTime) {
89 return $data->format($this->dateFormat);
92 if (is_object(
$data)) {
98 if (method_exists(
$data,
'__toString') && !
$data instanceof \JsonSerializable) {
99 $value = (string)
$data;
105 return sprintf(
"[object] (%s: %s)", get_class(
$data), $value);
108 if (is_resource(
$data)) {
112 return '[unknown('.gettype(
$data).
')]';
118 'class' => get_class($e),
119 'message' => $e->getMessage(),
120 'code' => $e->getCode(),
121 'file' => $e->getFile().
':'.$e->getLine(),
124 $trace = $e->getTrace();
125 foreach ($trace as $frame) {
126 if (isset($frame[
'file'])) {
127 $data[
'trace'][] = $frame[
'file'].
':'.$frame[
'line'];
134 if ($previous = $e->getPrevious()) {
145 if (version_compare(PHP_VERSION,
'5.4.0',
'>=')) {
146 return @json_encode(
$data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
149 return @json_encode(
$data);
152 if (version_compare(PHP_VERSION,
'5.4.0',
'>=')) {
153 $json = json_encode(
$data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
155 $json = json_encode(
$data);
158 if ($json ===
false) {
175 case JSON_ERROR_DEPTH:
176 $msg =
'Maximum stack depth exceeded';
178 case JSON_ERROR_STATE_MISMATCH:
179 $msg =
'Underflow or the modes mismatch';
181 case JSON_ERROR_CTRL_CHAR:
182 $msg =
'Unexpected control character found';
184 case JSON_ERROR_UTF8:
185 $msg =
'Malformed UTF-8 characters, possibly incorrectly encoded';
188 $msg =
'Unknown error';
191 throw new \RuntimeException(
'JSON encoding failed: '.$msg.
'. Encoding: '.var_export(
$data,
true));