* * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Monolog\Formatter; /** * Normalizes incoming records to remove objects/resources so it's easier to dump to various targets * * @author Jordi Boggiano */ class NormalizerFormatter implements FormatterInterface { const SIMPLE_DATE = "Y-m-d H:i:s"; protected $dateFormat; /** * @param string $dateFormat The format of the timestamp: one supported by DateTime::format */ public function __construct($dateFormat = null) { $this->dateFormat = $dateFormat ?: static::SIMPLE_DATE; } /** * {@inheritdoc} */ public function format(array $record) { return $this->normalize($record); } /** * {@inheritdoc} */ public function formatBatch(array $records) { foreach ($records as $key => $record) { $records[$key] = $this->format($record); } return $records; } protected function normalize($data) { if (null === $data || is_scalar($data)) { return $data; } if (is_array($data) || $data instanceof \Traversable) { $normalized = array(); foreach ($data as $key => $value) { $normalized[$key] = $this->normalize($value); } return $normalized; } if ($data instanceof \DateTime) { return $data->format($this->dateFormat); } if (is_resource($data)) { return '[resource]'; } return sprintf("[object] (%s: %s)", get_class($data), $this->toJson($data)); } protected function toJson($data) { if (version_compare(PHP_VERSION, '5.4.0', '>=')) { return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); } return json_encode($data); } }