| 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 |
* Formats incoming records into a one-line string |
| 14 |
* |
| 15 |
* This is especially useful for logging to files |
| 16 |
* |
| 17 |
* @author Jordi Boggiano <j.boggiano@seld.be> |
| 18 |
* @author Christophe Coevoet <stof@notk.org> |
| 19 |
*/ |
| 20 |
class Monolog_Formatter_LineFormatter extends Monolog_Formatter_NormalizerFormatter |
| 21 |
{ |
| 22 |
const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"; |
| 23 |
|
| 24 |
protected $format; |
| 25 |
|
| 26 |
/** |
| 27 |
* @param string $format The format of the message |
| 28 |
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format |
| 29 |
*/ |
| 30 |
public function __construct($format = null, $dateFormat = null) |
| 31 |
{ |
| 32 |
$this->format = $format ? $format : self::SIMPLE_FORMAT; |
| 33 |
parent::__construct($dateFormat); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* {@inheritdoc} |
| 38 |
*/ |
| 39 |
public function format(array $record) |
| 40 |
{ |
| 41 |
$vars = parent::format($record); |
| 42 |
|
| 43 |
$output = $this->format; |
| 44 |
foreach ($vars['extra'] as $var => $val) { |
| 45 |
if (false !== strpos($output, '%extra.'.$var.'%')) { |
| 46 |
$output = str_replace('%extra.'.$var.'%', $this->convertToString($val), $output); |
| 47 |
unset($vars['extra'][$var]); |
| 48 |
} |
| 49 |
} |
| 50 |
foreach ($vars as $var => $val) { |
| 51 |
if (false !== strpos($output, '%'.$var.'%')) { |
| 52 |
$output = str_replace('%'.$var.'%', $this->convertToString($val), $output); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
return $output; |
| 57 |
} |
| 58 |
|
| 59 |
public function formatBatch(array $records) |
| 60 |
{ |
| 61 |
$message = ''; |
| 62 |
foreach ($records as $record) { |
| 63 |
$message .= $this->format($record); |
| 64 |
} |
| 65 |
|
| 66 |
return $message; |
| 67 |
} |
| 68 |
|
| 69 |
protected function normalizeException(Exception $e) |
| 70 |
{ |
| 71 |
$previousText = ''; |
| 72 |
if (is_callable(array($e, 'getPrevious')) && $previous = $e->getPrevious()) { |
| 73 |
do { |
| 74 |
$previousText .= ', '.get_class($previous).': '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine(); |
| 75 |
} while ($previous = $previous->getPrevious()); |
| 76 |
} |
| 77 |
|
| 78 |
return '[object] ('.get_class($e).': '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')'; |
| 79 |
} |
| 80 |
|
| 81 |
protected function convertToString($data) |
| 82 |
{ |
| 83 |
if (null === $data || is_bool($data)) { |
| 84 |
return var_export($data, true); |
| 85 |
} |
| 86 |
|
| 87 |
if (is_scalar($data)) { |
| 88 |
return (string) $data; |
| 89 |
} |
| 90 |
|
| 91 |
if (version_compare(PHP_VERSION, '5.4.0', '>=')) { |
| 92 |
return $this->toJson($data, true); |
| 93 |
} |
| 94 |
|
| 95 |
return str_replace('\\/', '/', @json_encode($data)); |
| 96 |
} |
| 97 |
} |
| 98 |
|