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

Commit 1a617a3

Browse files
committed
bug #10 Enable dumping of objects in context, by unserializing them (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
2 parents 79104f1 + cddf305 commit 1a617a3

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/EasyLogFormatter.php

+26-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ class EasyLogFormatter implements FormatterInterface
2222
*/
2323
private $prefixLength = 2;
2424

25+
/**
26+
* @var string
27+
*/
28+
const PHP_SERIALIZED_OBJECT_PREFIX = '- !php/object:';
29+
2530
/**
2631
* @param array $record
2732
*
@@ -360,13 +365,33 @@ private function formatContext(array $record)
360365
$context = $this->filterVariablesUsedAsPlaceholders($record['message'], $record['context']);
361366
$context = $this->formatDateTimeObjects($context);
362367

363-
$contextAsString = Yaml::dump($context, $this->getInlineLevel($record), $this->prefixLength);
368+
$contextAsString = Yaml::dump($context, $this->getInlineLevel($record), $this->prefixLength, false, true);
369+
370+
if (substr($contextAsString, strpos($contextAsString, self::PHP_SERIALIZED_OBJECT_PREFIX), strlen(self::PHP_SERIALIZED_OBJECT_PREFIX)) === self::PHP_SERIALIZED_OBJECT_PREFIX) {
371+
$contextAsString = $this->formatSerializedObject($contextAsString);
372+
}
373+
364374
$contextAsString = $this->formatTextBlock($contextAsString, '--> ');
365375
$contextAsString = rtrim($contextAsString, PHP_EOL);
366376

367377
return $contextAsString;
368378
}
369379

380+
/**
381+
* @param $objectString
382+
*
383+
* @return string
384+
*/
385+
private function formatSerializedObject($objectString)
386+
{
387+
$objectPrefixLength = strlen(self::PHP_SERIALIZED_OBJECT_PREFIX);
388+
$objectStart = strpos($objectString, self::PHP_SERIALIZED_OBJECT_PREFIX) + $objectPrefixLength;
389+
$beforePrefix = substr($objectString, 0, $objectStart - $objectPrefixLength);
390+
$objectAsString = print_r(unserialize(substr($objectString, $objectStart)), true);
391+
392+
return $beforePrefix.$objectAsString;
393+
}
394+
370395
/**
371396
* @param array $record
372397
*

0 commit comments

Comments
 (0)