Skip to content

Commit 65c4fe5

Browse files
committed
Fix context normalizer
1 parent 272011e commit 65c4fe5

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/Monolog/Processor/ContextNormalizerProcessor.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace App\Monolog\Processor;
66

7+
use App\Entity\UserSerializableInterface;
78
use Monolog\Processor\ProcessorInterface;
89
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
910

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

2526
foreach ($record['context'] as $key => $value) {
26-
if ($this->normalizer->supportsNormalization($value, 'json')) {
27+
if (\is_object($value) && method_exists($value, '__toString')) {
28+
$record['context'][$key] = $value->__toString();
29+
} elseif ($value instanceof UserSerializableInterface) {
30+
$record['context'][$key] = json_encode($value->userSerialize()); // TODO use a custom normalizer
31+
} elseif ($this->normalizer->supportsNormalization($value, 'json')) {
2732
try {
2833
$record['context'][$key] = $this->normalizer->normalize($value, 'json', [
2934
'circular_reference_handler' => static function ($object) {
30-
return (string) $object;
35+
if (!\is_object($object)) {
36+
return null;
37+
}
38+
if (method_exists($object, '__toString')) {
39+
return $object->__toString();
40+
}
41+
if (!empty($object->id)) {
42+
return (string) $object->id;
43+
}
44+
45+
return null;
3146
},
3247
]);
3348
} catch (\Throwable $e) {

0 commit comments

Comments
 (0)