Skip to content

Commit

Permalink
Fix context normalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
mRoca committed May 15, 2020
1 parent 272011e commit 65c4fe5
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Monolog/Processor/ContextNormalizerProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Monolog\Processor;

use App\Entity\UserSerializableInterface;
use Monolog\Processor\ProcessorInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

Expand All @@ -23,11 +24,25 @@ public function __invoke(array $record): array
}

foreach ($record['context'] as $key => $value) {
if ($this->normalizer->supportsNormalization($value, 'json')) {
if (\is_object($value) && method_exists($value, '__toString')) {
$record['context'][$key] = $value->__toString();
} elseif ($value instanceof UserSerializableInterface) {
$record['context'][$key] = json_encode($value->userSerialize()); // TODO use a custom normalizer
} elseif ($this->normalizer->supportsNormalization($value, 'json')) {
try {
$record['context'][$key] = $this->normalizer->normalize($value, 'json', [
'circular_reference_handler' => static function ($object) {
return (string) $object;
if (!\is_object($object)) {
return null;
}
if (method_exists($object, '__toString')) {
return $object->__toString();
}
if (!empty($object->id)) {
return (string) $object->id;
}

return null;
},
]);
} catch (\Throwable $e) {
Expand Down

0 comments on commit 65c4fe5

Please sign in to comment.