From 62c57abf935d86dfc3a6de987fd13dfae4f19f69 Mon Sep 17 00:00:00 2001 From: Andre Smith Date: Sat, 28 Oct 2017 01:13:38 +0200 Subject: [PATCH 1/2] Enable dumping of objects in context, by unserializing them --- src/EasyLogFormatter.php | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/EasyLogFormatter.php b/src/EasyLogFormatter.php index a75995a..5180a0f 100644 --- a/src/EasyLogFormatter.php +++ b/src/EasyLogFormatter.php @@ -22,6 +22,11 @@ class EasyLogFormatter implements FormatterInterface */ private $prefixLength = 2; + /** + * @var string + */ + const PHP_SERIALIZED_OBJECT_PREFIX = '- !php/object:'; + /** * @param array $record * @@ -360,13 +365,32 @@ 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 * From cddf30504264f1f72f73ae8e5b1e5884b91672c3 Mon Sep 17 00:00:00 2001 From: Javier Eguiluz Date: Wed, 10 Jan 2018 09:33:56 +0100 Subject: [PATCH 2/2] Fixed code syntax issues --- src/EasyLogFormatter.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/EasyLogFormatter.php b/src/EasyLogFormatter.php index 5180a0f..33d2b65 100644 --- a/src/EasyLogFormatter.php +++ b/src/EasyLogFormatter.php @@ -382,13 +382,14 @@ private function formatContext(array $record) * * @return string */ - private function formatSerializedObject($objectString) { + 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; + return $beforePrefix.$objectAsString; } /**