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

Commit

Permalink
bug #10 Enable dumping of objects in context, by unserializing them (…
Browse files Browse the repository at this point in the history
…Andre Smith, javiereguiluz)

This PR was merged into the master branch.

Discussion
----------

Enable dumping of objects in context, by unserializing them

Enable ObjectSupport for context data in order to get output in the logs when the context contains objects.

Commits
-------

cddf305 Fixed code syntax issues
62c57ab Enable dumping of objects in context, by unserializing them
  • Loading branch information
javiereguiluz committed Jan 10, 2018
2 parents 79104f1 + cddf305 commit 1a617a3
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/EasyLogFormatter.php
Expand Up @@ -22,6 +22,11 @@ class EasyLogFormatter implements FormatterInterface
*/
private $prefixLength = 2;

/**
* @var string
*/
const PHP_SERIALIZED_OBJECT_PREFIX = '- !php/object:';

/**
* @param array $record
*
Expand Down Expand Up @@ -360,13 +365,33 @@ 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 = Yaml::dump($context, $this->getInlineLevel($record), $this->prefixLength, false, true);

if (substr($contextAsString, strpos($contextAsString, self::PHP_SERIALIZED_OBJECT_PREFIX), strlen(self::PHP_SERIALIZED_OBJECT_PREFIX)) === self::PHP_SERIALIZED_OBJECT_PREFIX) {
$contextAsString = $this->formatSerializedObject($contextAsString);
}

$contextAsString = $this->formatTextBlock($contextAsString, '--> ');
$contextAsString = rtrim($contextAsString, PHP_EOL);

return $contextAsString;
}

/**
* @param $objectString
*
* @return string
*/
private function formatSerializedObject($objectString)
{
$objectPrefixLength = strlen(self::PHP_SERIALIZED_OBJECT_PREFIX);
$objectStart = strpos($objectString, self::PHP_SERIALIZED_OBJECT_PREFIX) + $objectPrefixLength;
$beforePrefix = substr($objectString, 0, $objectStart - $objectPrefixLength);
$objectAsString = print_r(unserialize(substr($objectString, $objectStart)), true);

return $beforePrefix.$objectAsString;
}

/**
* @param array $record
*
Expand Down

0 comments on commit 1a617a3

Please sign in to comment.