1616
1717use Illuminate \Contracts \Config \Repository ;
1818use Illuminate \Database \Events \QueryExecuted ;
19+ use Illuminate \Support \Arr ;
1920use Psr \Log \LoggerInterface ;
2021
2122class QueryLogger
@@ -29,7 +30,7 @@ public function __construct(LoggerInterface $logger, Repository $config)
2930 $ this ->config = $ config ;
3031 }
3132
32- public function handle (QueryExecuted $ event )
33+ public function handle (QueryExecuted $ event ): void
3334 {
3435 $ pdo = \method_exists ($ event ->connection , 'getPdo ' )
3536 ? $ event ->connection ->getPdo ()
@@ -45,6 +46,7 @@ public function handle(QueryExecuted $event)
4546 'time ' => $ event ->time ,
4647 'connection ' => $ event ->connectionName ,
4748 'database ' => $ this ->config ->get ("database.connections. {$ event ->connectionName }.database " ),
49+ 'callSpot ' => $ this ->guessCallSpot (),
4850 ]);
4951 }
5052
@@ -106,4 +108,18 @@ protected function quote(?\PDO $pdo, string $value): string
106108
107109 return "' " . \str_replace ($ search , $ replace , $ value ) . "' " ;
108110 }
111+
112+ protected function guessCallSpot (): array
113+ {
114+ $ stack = \debug_backtrace (\DEBUG_BACKTRACE_IGNORE_ARGS );
115+ $ vendor = \DIRECTORY_SEPARATOR . 'vendor ' . \DIRECTORY_SEPARATOR ;
116+
117+ foreach ($ stack as $ trace ) {
118+ if (! \str_contains ($ trace ['file ' ], $ vendor )) {
119+ return Arr::only ($ trace , ['file ' , 'line ' , 'function ' ]);
120+ }
121+ }
122+
123+ return ['file ' => null , 'line ' => null , 'function ' => null ];
124+ }
109125}
0 commit comments