Source of file NormalizerFormatter.php
Size: 2,532 Bytes - Last Modified: 2013-04-23T12:09:48+02:00
/home/theseer/Downloads/Symfony/vendor/monolog/monolog/src/Monolog/Formatter/NormalizerFormatter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 | <?php /* * This file is part of the Monolog package. * * (c) Jordi Boggiano <j.boggiano@seld.be> * * 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 <j.boggiano@seld.be> */ 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_object($data)) { return sprintf("[object] (%s: %s)", get_class($data), $this->toJson($data, true)); } if (is_resource($data)) { return '[resource]'; } return '[unknown('.gettype($data).')]'; } protected function toJson($data, $ignoreErrors = false) { // suppress json_encode errors since it's twitchy with some inputs if ($ignoreErrors) { if (version_compare(PHP_VERSION, '5.4.0', '>=')) { return @json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); } return @json_encode($data); } if (version_compare(PHP_VERSION, '5.4.0', '>=')) { return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); } return json_encode($data); } } |