From c379ddb87c70487e8778ba03f799836c92ff1592 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 31 Aug 2016 12:38:28 +0200 Subject: [PATCH] Fixed the handling of DateTime objects --- src/EasyLogFormatter.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/EasyLogFormatter.php b/src/EasyLogFormatter.php index b102832..f5f27a1 100644 --- a/src/EasyLogFormatter.php +++ b/src/EasyLogFormatter.php @@ -265,6 +265,7 @@ private function formatLogChannel($record) private function formatContext(array $record) { $context = $this->filterVariablesUsedAsPlaceholders($record['message'], $record['context']); + $context = $this->formatDateTimeObjects($context); $contextAsString = Yaml::dump($context, $this->getInlineLevel($record), $this->prefixLength); $contextAsString = $this->formatTextBlock($contextAsString, '--> '); @@ -275,7 +276,8 @@ private function formatContext(array $record) private function formatExtra(array $record) { - $extraAsString = Yaml::dump(array('extra' => $record['extra']), 1, $this->prefixLength); + $extra = $this->formatDateTimeObjects($record['extra']); + $extraAsString = Yaml::dump(array('extra' => $extra), 1, $this->prefixLength); $extraAsString = $this->formatTextBlock($extraAsString, '--> '); return $extraAsString; @@ -475,6 +477,24 @@ private function formatTextBlock($text, $prefix = '', $prefixAllLines = false) return implode(PHP_EOL, $newTextLines).($addTrailingNewline ? PHP_EOL : ''); } + /** + * Turns any DateTime object present in the given array into a string + * representation of that date and time. + * + * @param array $array + * @return array + */ + private function formatDateTimeObjects(array $array) + { + array_walk_recursive($array, function (&$value) { + if ($value instanceof \DateTimeInterface) { + $value = date_format($value, 'c'); + } + }); + + return $array; + } + /** * It scans the given string for placeholders and removes from $variables * any element whose key matches the name of a placeholder.