From a8d4fc4a34c6abc9928735d56aa5653b5d0834d7 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Tue, 31 May 2011 16:38:48 +0200 Subject: [PATCH] Tweaked the formatting of complex data in the LineFormatter --- src/Monolog/Formatter/FormatterInterface.php | 6 +-- src/Monolog/Formatter/LineFormatter.php | 42 +++++++++++++------- 2 files changed, 30 insertions(+), 18 deletions(-) diff --git a/src/Monolog/Formatter/FormatterInterface.php b/src/Monolog/Formatter/FormatterInterface.php index f32226384..77891de80 100644 --- a/src/Monolog/Formatter/FormatterInterface.php +++ b/src/Monolog/Formatter/FormatterInterface.php @@ -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); } diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index 7c6c3dcd6..63fc119c5 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -19,6 +19,7 @@ * This is especially useful for logging to files * * @author Jordi Boggiano + * @author Christophe Coevoet */ class LineFormatter implements FormatterInterface { @@ -47,20 +48,12 @@ public function format(array $record) $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 @@ public function formatBatch(array $records) 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)); } }