@@ -18,18 +18,29 @@ public function __construct(
1818
1919 public function format (LogRecord $ record ): array
2020 {
21- $ exception = $ record ->context ['exception ' ] ?? null ;
22- if (! $ exception instanceof Throwable) {
21+ $ exceptionData = $ record ->context ['exception ' ] ?? null ;
22+
23+ // Handle case where the exception is stored as a string instead of a Throwable object
24+ if (is_string ($ exceptionData ) &&
25+ (str_contains ($ exceptionData , 'Stack trace: ' ) || preg_match ('/#\d+ \// ' , $ exceptionData ))) {
26+
27+ return $ this ->formatExceptionString ($ exceptionData );
28+ }
29+
30+ // Original code for Throwable objects
31+ if (! $ exceptionData instanceof Throwable) {
2332 return [];
2433 }
2534
26- $ header = $ this ->formatHeader ($ exception );
27- $ stackTrace = $ exception ->getTraceAsString ();
35+ $ message = $ this ->formatMessage ($ exceptionData ->getMessage ());
36+ $ stackTrace = $ exceptionData ->getTraceAsString ();
37+
38+ $ header = $ this ->formatHeader ($ exceptionData );
2839
2940 return [
30- 'message ' => $ exception -> getMessage () ,
31- 'simplified_stack_trace ' => $ header ."\n[stacktrace] \n" .$ this ->stackTraceFormatter ->format ($ stackTrace ),
32- 'full_stack_trace ' => $ header ."\n[stacktrace] \n" .$ stackTrace ,
41+ 'message ' => $ message ,
42+ 'simplified_stack_trace ' => $ header ."\n[stacktrace] \n" .$ this ->stackTraceFormatter ->format ($ stackTrace, true ),
43+ 'full_stack_trace ' => $ header ."\n[stacktrace] \n" .$ this -> stackTraceFormatter -> format ( $ stackTrace, false ) ,
3344 ];
3445 }
3546
@@ -52,6 +63,15 @@ public function formatTitle(Throwable $exception, string $level): string
5263 ->toString ();
5364 }
5465
66+ private function formatMessage (string $ message ): string
67+ {
68+ if (! str_contains ($ message , 'Stack trace: ' )) {
69+ return $ message ;
70+ }
71+
72+ return (string ) preg_replace ('/\s+in\s+\/[^\s]+\.php:\d+.*$/s ' , '' , $ message );
73+ }
74+
5575 private function formatHeader (Throwable $ exception ): string
5676 {
5777 return sprintf (
@@ -63,4 +83,48 @@ private function formatHeader(Throwable $exception): string
6383 $ exception ->getLine ()
6484 );
6585 }
86+
87+ /**
88+ * Format an exception stored as a string.
89+ */
90+ private function formatExceptionString (string $ exceptionString ): array
91+ {
92+ $ message = $ exceptionString ;
93+ $ stackTrace = '' ;
94+
95+ // Try to extract the message and stack trace
96+ if (preg_match ('/^(.*?)(?:Stack trace:|#\d+ \/)/ ' , $ exceptionString , $ matches )) {
97+ $ message = trim ($ matches [1 ]);
98+
99+ // Remove file/line info if present
100+ if (preg_match ('/^(.*) in \/[^\s]+(?:\.php)? on line \d+$/s ' , $ message , $ fileMatches )) {
101+ $ message = trim ($ fileMatches [1 ]);
102+ }
103+
104+ // Extract stack trace
105+ $ traceStart = strpos ($ exceptionString , 'Stack trace: ' );
106+ if ($ traceStart === false ) {
107+ // Find the first occurrence of a stack frame pattern
108+ if (preg_match ('/#\d+ \// ' , $ exceptionString , $ matches , PREG_OFFSET_CAPTURE )) {
109+ $ traceStart = $ matches [0 ][1 ];
110+ }
111+ }
112+
113+ if ($ traceStart !== false ) {
114+ $ stackTrace = substr ($ exceptionString , $ traceStart );
115+ }
116+ }
117+
118+ $ header = sprintf (
119+ '[%s] Exception: %s at unknown:0 ' ,
120+ now ()->format ('Y-m-d H:i:s ' ),
121+ $ message
122+ );
123+
124+ return [
125+ 'message ' => $ this ->formatMessage ($ message ),
126+ 'simplified_stack_trace ' => $ header ."\n[stacktrace] \n" .$ this ->stackTraceFormatter ->format ($ stackTrace , true ),
127+ 'full_stack_trace ' => $ header ."\n[stacktrace] \n" .$ this ->stackTraceFormatter ->format ($ stackTrace , false ),
128+ ];
129+ }
66130}
0 commit comments