@@ -364,8 +364,9 @@ private function formatContext(array $record)
364
364
{
365
365
$ context = $ this ->filterVariablesUsedAsPlaceholders ($ record ['message ' ], $ record ['context ' ]);
366
366
$ context = $ this ->formatDateTimeObjects ($ context );
367
+ $ context = $ this ->formatThrowableObjects ($ context );
367
368
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 );
369
370
370
371
if (substr ($ contextAsString , strpos ($ contextAsString , self ::PHP_SERIALIZED_OBJECT_PREFIX ), strlen (self ::PHP_SERIALIZED_OBJECT_PREFIX )) === self ::PHP_SERIALIZED_OBJECT_PREFIX ) {
371
372
$ contextAsString = $ this ->formatSerializedObject ($ contextAsString );
@@ -377,6 +378,43 @@ private function formatContext(array $record)
377
378
return $ contextAsString ;
378
379
}
379
380
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
+
380
418
/**
381
419
* @param $objectString
382
420
*
0 commit comments