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

Commit 9b428a3

Browse files
committedApr 19, 2018
Fix formatter
1 parent 1a617a3 commit 9b428a3

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed
 

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/composer.lock
2+
/vendor/

‎src/EasyLogFormatter.php

+39-1
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)
This repository has been archived.