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

Commit 21c442f

Browse files
committed
bug #12 Fix formatter (theofidry)
This PR was merged into the master branch. Discussion ---------- Fix formatter Closes #11 The fix is not perfect as this won't work if another kind of PHP object is passed in the context, how this should handle the most common case. The idea is like for the dates, we format the exceptions by serializing them. However if the serialization fails, e.g. because an unserializable class is found in the stack trace like a `Closure`, we transform the exception into a normalized array based on the `Throwable` API. A few other things have been included: - Added a `.gitignore` project: while `vendor` is debatable `composer.lock` is less, in any case I think there is little harm in including this. - Replaced the usage of deprecated parameters in `Yaml::dump()` in favour of their new equivalent `Yaml::DUMP_OBJECT` Note that I only tested locally on my project as there is not proper test on the repo. Commits ------- 9b428a3 Fix formatter
2 parents 1a617a3 + 9b428a3 commit 21c442f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/composer.lock
2+
/vendor/

src/EasyLogFormatter.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,9 @@ private function formatContext(array $record)
364364
{
365365
$context = $this->filterVariablesUsedAsPlaceholders($record['message'], $record['context']);
366366
$context = $this->formatDateTimeObjects($context);
367+
$context = $this->formatThrowableObjects($context);
367368

368-
$contextAsString = Yaml::dump($context, $this->getInlineLevel($record), $this->prefixLength, false, true);
369+
$contextAsString = Yaml::dump($context, $this->getInlineLevel($record), $this->prefixLength, Yaml::DUMP_OBJECT);
369370

370371
if (substr($contextAsString, strpos($contextAsString, self::PHP_SERIALIZED_OBJECT_PREFIX), strlen(self::PHP_SERIALIZED_OBJECT_PREFIX)) === self::PHP_SERIALIZED_OBJECT_PREFIX) {
371372
$contextAsString = $this->formatSerializedObject($contextAsString);
@@ -377,6 +378,43 @@ private function formatContext(array $record)
377378
return $contextAsString;
378379
}
379380

381+
/**
382+
* Turns any Throwable object present in the given array into a string
383+
* representation. If the object cannot be serialized, an approximative
384+
* representation of the object is given instead.
385+
*
386+
* @param array $array
387+
*
388+
* @return array
389+
*/
390+
private function formatThrowableObjects(array $array): array
391+
{
392+
array_walk_recursive($array, function (&$value) {
393+
if ($value instanceof \Throwable) {
394+
try {
395+
$value = serialize($value);
396+
} catch (\Throwable $throwable) {
397+
$value = $this->formatThrowable($value);
398+
}
399+
}
400+
});
401+
402+
return $array;
403+
}
404+
405+
private function formatThrowable(Throwable $throwable): array
406+
{
407+
return [
408+
'class' => get_class($throwable),
409+
'message' => $throwable->getMessage(),
410+
'code' => $throwable->getCode(),
411+
'file' => $throwable->getFile(),
412+
'line' => $throwable->getLine(),
413+
'trace' => $throwable->getTraceAsString(),
414+
'previous' => $throwable->getPrevious() ? $this->formatThrowable($throwable->getPrevious()) : null,
415+
];
416+
}
417+
380418
/**
381419
* @param $objectString
382420
*

0 commit comments

Comments
 (0)