1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-01 19:00:20 +02:00

Tweaked the formatting of complex data in the LineFormatter

This commit is contained in:
Christophe Coevoet
2011-05-31 16:38:48 +02:00
parent 53c9b2050f
commit a8d4fc4a34
2 changed files with 30 additions and 18 deletions

View File

@@ -22,15 +22,15 @@ interface FormatterInterface
* Formats a log record.
*
* @param array $record A record to format
* @return string The formatted message
* @return mixed The formatted record
*/
function format(array $record);
/**
* Formats a set of log records.
*
* @param array $record A record to format
* @return string The formatted batch message
* @param array $records A set of records to format
* @return mixed The formatted set of records
*/
function formatBatch(array $records);
}

View File

@@ -19,6 +19,7 @@ use Monolog\Logger;
* This is especially useful for logging to files
*
* @author Jordi Boggiano <j.boggiano@seld.be>
* @author Christophe Coevoet <stof@notk.org>
*/
class LineFormatter implements FormatterInterface
{
@@ -47,20 +48,12 @@ class LineFormatter implements FormatterInterface
$vars['datetime'] = $vars['datetime']->format($this->dateFormat);
$output = $this->format;
foreach ($vars as $var => $val) {
if (is_array($val)) {
$strval = array();
foreach ($val as $subvar => $subval) {
$strval[] = $subvar.': '.$this->convertToString($subval);
}
$replacement = $strval ? $var.'('.implode(', ', $strval).')' : '';
$output = str_replace('%'.$var.'%', $replacement, $output);
} else {
$output = str_replace('%'.$var.'%', $this->convertToString($val), $output);
}
}
foreach ($vars['extra'] as $var => $val) {
$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;
@@ -76,12 +69,31 @@ class LineFormatter implements FormatterInterface
return $message;
}
private function convertToString($data)
protected function convertToString($data)
{
if (is_scalar($data) || (is_object($data) && method_exists($data, '__toString'))) {
if (null === $data || is_scalar($data)) {
return (string) $data;
}
return serialize($data);
return json_encode($this->normalize($data));
}
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;
}
return sprintf("[object] (%s: %s)", get_class($data), json_decode($data));
}
}