1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-21 08:36:33 +02:00

Added support of objects and arrays as values in LineFormatter. closes #19

This commit is contained in:
Christophe Coevoet
2011-05-08 19:28:01 +02:00
parent 0ebdf69daa
commit 0775b03de0
2 changed files with 60 additions and 3 deletions

View File

@@ -51,16 +51,16 @@ class LineFormatter implements FormatterInterface
if (is_array($val)) {
$strval = array();
foreach ($val as $subvar => $subval) {
$strval[] = $subvar.': '.$subval;
$strval[] = $subvar.': '.$this->convertToString($subval);
}
$replacement = $strval ? $var.'('.implode(', ', $strval).')' : '';
$output = str_replace('%'.$var.'%', $replacement, $output);
} else {
$output = str_replace('%'.$var.'%', $val, $output);
$output = str_replace('%'.$var.'%', $this->convertToString($val), $output);
}
}
foreach ($vars['extra'] as $var => $val) {
$output = str_replace('%extra.'.$var.'%', $val, $output);
$output = str_replace('%extra.'.$var.'%', $this->convertToString($val), $output);
}
return $output;
@@ -75,4 +75,13 @@ class LineFormatter implements FormatterInterface
return $message;
}
private function convertToString($data)
{
if (is_scalar($data) || (is_object($data) && method_exists($data, '__toString'))) {
return (string) $data;
}
return serialize($data);
}
}