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

Commit

Permalink
bug #6 Fixed the handling of DateTime objects (javiereguiluz)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
javiereguiluz committed Aug 31, 2016
2 parents fe1c922 + c379ddb commit 09d4538
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/EasyLogFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, '--> ');
Expand All @@ -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;
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 09d4538

Please sign in to comment.