@@ -54,9 +54,21 @@ public function recordLog(MessageLogged $log): void
5454            // Should this fail, we should continue to emit the LogRecord. 
5555        }
5656
57+         $ contextToEncode  = array_filter ($ log ->context );
58+ 
59+         $ exception  = $ this  ->getExceptionFromContext ($ log ->context );
60+ 
61+         if  ($ exception  !== null ) {
62+             unset($ contextToEncode ['exception ' ]);
63+         }
64+ 
5765        $ attributes  = [
58-             'context '  => json_encode (array_filter ($ log ->context )),
59-             ...$ this  ->parseExceptionFromContext ($ log ->context ),
66+             'context '  => json_encode ($ contextToEncode ),
67+             ...$ exception  !== null  ? [
68+                 'exception.type '  => $ exception ::class,
69+                 'exception.message '  => $ exception ->getMessage (),
70+                 'exception.stacktrace '  => StackTraceFormatter::format ($ exception ),
71+             ] : [],
6072        ];
6173
6274        $ logger  = $ this  ->instrumentation ->logger ();
@@ -69,21 +81,15 @@ public function recordLog(MessageLogged $log): void
6981        $ logger ->emit ($ record );
7082    }
7183
72-     private  function  parseExceptionFromContext (array  $ context ): array 
84+     private  function  getExceptionFromContext (array  $ context ): ? Throwable 
7385    {
7486        if  (
7587            ! isset ($ context ['exception ' ]) ||
7688            ! $ context ['exception ' ] instanceof  Throwable
7789        ) {
78-             return  [] ;
90+             return  null ;
7991        }
8092
81-         $ exception  = $ context ['exception ' ];
82- 
83-         return  [
84-             'exception.type '  => $ exception ::class,
85-             'exception.message '  => $ exception ->getMessage (),
86-             'exception.stacktrace '  => StackTraceFormatter::format ($ exception ),
87-         ];
93+         return  $ context ['exception ' ];
8894    }
8995}
0 commit comments