@@ -18,18 +18,29 @@ public function __construct(
18
18
19
19
public function format (LogRecord $ record ): array
20
20
{
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) {
23
32
return [];
24
33
}
25
34
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 );
28
39
29
40
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 ) ,
33
44
];
34
45
}
35
46
@@ -52,6 +63,15 @@ public function formatTitle(Throwable $exception, string $level): string
52
63
->toString ();
53
64
}
54
65
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
+
55
75
private function formatHeader (Throwable $ exception ): string
56
76
{
57
77
return sprintf (
@@ -63,4 +83,48 @@ private function formatHeader(Throwable $exception): string
63
83
$ exception ->getLine ()
64
84
);
65
85
}
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
+ }
66
130
}
0 commit comments