| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Monolog package. |
| 5 |
* |
| 6 |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 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 Monolog_Formatter_NormalizerFormatter implements Monolog_Formatter_FormatterInterface |
| 18 |
{ |
| 19 |
const SIMPLE_DATE = "Y-m-d H:i:s"; |
| 20 |
|
| 21 |
protected $dateFormat; |
| 22 |
|
| 23 |
/** |
| 24 |
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format |
| 25 |
*/ |
| 26 |
public function __construct($dateFormat = null) |
| 27 |
{ |
| 28 |
$this->dateFormat = $dateFormat ? $dateFormat : self::SIMPLE_DATE; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* {@inheritdoc} |
| 33 |
*/ |
| 34 |
public function format(array $record) |
| 35 |
{ |
| 36 |
return $this->normalize($record); |
| 37 |
} |
| 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 |
|
| 48 |
return $records; |
| 49 |
} |
| 50 |
|
| 51 |
protected function normalize($data) |
| 52 |
{ |
| 53 |
if (null === $data || is_scalar($data)) { |
| 54 |
return $data; |
| 55 |
} |
| 56 |
|
| 57 |
if (is_array($data) || $data instanceof Traversable) { |
| 58 |
$normalized = array(); |
| 59 |
|
| 60 |
$count = 1; |
| 61 |
foreach ($data as $key => $value) { |
| 62 |
if ($count++ >= 1000) { |
| 63 |
$normalized['...'] = 'Over 1000 items, aborting normalization'; |
| 64 |
break; |
| 65 |
} |
| 66 |
$normalized[$key] = $this->normalize($value); |
| 67 |
} |
| 68 |
|
| 69 |
return $normalized; |
| 70 |
} |
| 71 |
|
| 72 |
if ($data instanceof DateTime) { |
| 73 |
return $data->format($this->dateFormat); |
| 74 |
} |
| 75 |
|
| 76 |
if (is_object($data)) { |
| 77 |
if ($data instanceof Exception) { |
| 78 |
return $this->normalizeException($data); |
| 79 |
} |
| 80 |
|
| 81 |
return sprintf("[object] (%s: %s)", get_class($data), $this->toJson($data, true)); |
| 82 |
} |
| 83 |
|
| 84 |
if (is_resource($data)) { |
| 85 |
return '[resource]'; |
| 86 |
} |
| 87 |
|
| 88 |
return '[unknown('.gettype($data).')]'; |
| 89 |
} |
| 90 |
|
| 91 |
protected function normalizeException(Exception $e) |
| 92 |
{ |
| 93 |
$data = array( |
| 94 |
'class' => get_class($e), |
| 95 |
'message' => $e->getMessage(), |
| 96 |
'file' => $e->getFile().':'.$e->getLine(), |
| 97 |
); |
| 98 |
|
| 99 |
$trace = $e->getTrace(); |
| 100 |
array_shift($trace); |
| 101 |
foreach ($trace as $frame) { |
| 102 |
if (isset($frame['file'])) { |
| 103 |
$data['trace'][] = $frame['file'].':'.$frame['line']; |
| 104 |
} else { |
| 105 |
$data['trace'][] = json_encode($frame); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
$previous = null; |
| 110 |
|
| 111 |
if (is_callable(array($e, 'getPrevious'))) { |
| 112 |
$previous = $e->getPrevious(); |
| 113 |
} elseif (is_callable($e, 'getPreviousException')) { |
| 114 |
$previous = $e->getPreviousException(); |
| 115 |
} |
| 116 |
|
| 117 |
if ($previous) { |
| 118 |
$data['previous'] = $this->normalizeException($previous); |
| 119 |
} |
| 120 |
|
| 121 |
return $data; |
| 122 |
} |
| 123 |
|
| 124 |
protected function toJson($data, $ignoreErrors = false) |
| 125 |
{ |
| 126 |
// suppress json_encode errors since it's twitchy with some inputs |
| 127 |
if ($ignoreErrors) { |
| 128 |
if (version_compare(PHP_VERSION, '5.4.0', '>=')) { |
| 129 |
return @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
| 130 |
} |
| 131 |
|
| 132 |
return @json_encode($data); |
| 133 |
} |
| 134 |
|
| 135 |
if (version_compare(PHP_VERSION, '5.4.0', '>=')) { |
| 136 |
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
| 137 |
} |
| 138 |
|
| 139 |
return json_encode($data); |
| 140 |
} |
| 141 |
} |
| 142 |
|