Skip to content
This repository was archived by the owner on Apr 7, 2020. It is now read-only.

Commit 09d4538

Browse files
committed
bug #6 Fixed the handling of DateTime objects (javiereguiluz)
This PR was merged into the master branch. Discussion ---------- Fixed the handling of DateTime objects This fixes #5. Commits ------- c379ddb Fixed the handling of DateTime objects
2 parents fe1c922 + c379ddb commit 09d4538

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

src/EasyLogFormatter.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ private function formatLogChannel($record)
265265
private function formatContext(array $record)
266266
{
267267
$context = $this->filterVariablesUsedAsPlaceholders($record['message'], $record['context']);
268+
$context = $this->formatDateTimeObjects($context);
268269

269270
$contextAsString = Yaml::dump($context, $this->getInlineLevel($record), $this->prefixLength);
270271
$contextAsString = $this->formatTextBlock($contextAsString, '--> ');
@@ -275,7 +276,8 @@ private function formatContext(array $record)
275276

276277
private function formatExtra(array $record)
277278
{
278-
$extraAsString = Yaml::dump(array('extra' => $record['extra']), 1, $this->prefixLength);
279+
$extra = $this->formatDateTimeObjects($record['extra']);
280+
$extraAsString = Yaml::dump(array('extra' => $extra), 1, $this->prefixLength);
279281
$extraAsString = $this->formatTextBlock($extraAsString, '--> ');
280282

281283
return $extraAsString;
@@ -475,6 +477,24 @@ private function formatTextBlock($text, $prefix = '', $prefixAllLines = false)
475477
return implode(PHP_EOL, $newTextLines).($addTrailingNewline ? PHP_EOL : '');
476478
}
477479

480+
/**
481+
* Turns any DateTime object present in the given array into a string
482+
* representation of that date and time.
483+
*
484+
* @param array $array
485+
* @return array
486+
*/
487+
private function formatDateTimeObjects(array $array)
488+
{
489+
array_walk_recursive($array, function (&$value) {
490+
if ($value instanceof \DateTimeInterface) {
491+
$value = date_format($value, 'c');
492+
}
493+
});
494+
495+
return $array;
496+
}
497+
478498
/**
479499
* It scans the given string for placeholders and removes from $variables
480500
* any element whose key matches the name of a placeholder.

0 commit comments

Comments
 (0)