Source of file LineFormatter.php
Size: 2,907 Bytes - Last Modified: 2013-04-23T12:09:48+02:00
/home/theseer/Downloads/Symfony/vendor/monolog/monolog/src/Monolog/Formatter/LineFormatter.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 | <?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; /** * Formats incoming records into a one-line string * * This is especially useful for logging to files * * @author Jordi Boggiano <j.boggiano@seld.be> * @author Christophe Coevoet <stof@notk.org> */ class LineFormatter extends NormalizerFormatter { const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"; protected $format; /** * @param string $format The format of the message * @param string $dateFormat The format of the timestamp: one supported by DateTime::format */ public function __construct($format = null, $dateFormat = null) { $this->format = $format ?: static::SIMPLE_FORMAT; parent::__construct($dateFormat); } /** * {@inheritdoc} */ public function format(array $record) { $vars = parent::format($record); $output = $this->format; foreach ($vars['extra'] as $var => $val) { if (false !== strpos($output, '%extra.'.$var.'%')) { $output = str_replace('%extra.'.$var.'%', $this->convertToString($val), $output); unset($vars['extra'][$var]); } } foreach ($vars as $var => $val) { $output = str_replace('%'.$var.'%', $this->convertToString($val), $output); } return $output; } public function formatBatch(array $records) { $message = ''; foreach ($records as $record) { $message .= $this->format($record); } return $message; } protected function normalize($data) { if (is_bool($data) || is_null($data)) { return var_export($data, true); } if ($data instanceof \Exception) { $previousText = ''; if ($previous = $data->getPrevious()) { do { $previousText .= ', '.get_class($previous).': '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine(); } while ($previous = $previous->getPrevious()); } return '[object] ('.get_class($data).': '.$data->getMessage().' at '.$data->getFile().':'.$data->getLine().$previousText.')'; } return parent::normalize($data); } protected function convertToString($data) { if (null === $data || is_scalar($data)) { return (string) $data; } $data = $this->normalize($data); if (version_compare(PHP_VERSION, '5.4.0', '>=')) { return $this->toJson($data); } return str_replace('\\/', '/', json_encode($data)); } } |