@@ -32,6 +32,16 @@ class Handler extends ExceptionHandler
3232 ValidationException::class,
3333 ];
3434
35+ /**
36+ * A list of exceptions that should be logged with cleaned stack
37+ * traces to avoid exposing credentials or other sensitive information.
38+ *
39+ * @var array
40+ */
41+ protected $ cleanStacks = [
42+ PDOException::class,
43+ ];
44+
3545 /**
3646 * A list of the inputs that are never flashed for validation exceptions.
3747 *
@@ -73,7 +83,40 @@ public function report(Exception $exception)
7383 throw $ exception ;
7484 }
7585
76- return $ logger ->error ($ exception instanceof PDOException ? $ exception ->getMessage () : $ exception );
86+ foreach ($ this ->cleanStacks as $ class ) {
87+ if ($ exception instanceof $ class ) {
88+ $ exception = $ this ->generateCleanedExceptionStack ($ exception );
89+ break ;
90+ }
91+ }
92+
93+ return $ logger ->error ($ exception );
94+ }
95+
96+ private function generateCleanedExceptionStack (Exception $ exception )
97+ {
98+ $ cleanedStack = '' ;
99+ foreach ($ exception ->getTrace () as $ index => $ item ) {
100+ $ cleanedStack .= sprintf (
101+ "#%d %s(%d): %s%s%s \n" ,
102+ $ index ,
103+ array_get ($ item , 'file ' ),
104+ array_get ($ item , 'line ' ),
105+ array_get ($ item , 'class ' ),
106+ array_get ($ item , 'type ' ),
107+ array_get ($ item , 'function ' )
108+ );
109+ }
110+
111+ $ message = sprintf (
112+ '%s: %s in %s:%d ' ,
113+ class_basename ($ exception ),
114+ $ exception ->getMessage (),
115+ $ exception ->getFile (),
116+ $ exception ->getLine ()
117+ );
118+
119+ return $ message . "\nStack trace: \n" . trim ($ cleanedStack );
77120 }
78121
79122 /**
0 commit comments