16
16
17
17
use Illuminate \Contracts \Config \Repository ;
18
18
use Illuminate \Database \Events \QueryExecuted ;
19
+ use Illuminate \Support \Arr ;
19
20
use Psr \Log \LoggerInterface ;
20
21
21
22
class QueryLogger
@@ -29,7 +30,7 @@ public function __construct(LoggerInterface $logger, Repository $config)
29
30
$ this ->config = $ config ;
30
31
}
31
32
32
- public function handle (QueryExecuted $ event )
33
+ public function handle (QueryExecuted $ event ): void
33
34
{
34
35
$ pdo = \method_exists ($ event ->connection , 'getPdo ' )
35
36
? $ event ->connection ->getPdo ()
@@ -45,6 +46,7 @@ public function handle(QueryExecuted $event)
45
46
'time ' => $ event ->time ,
46
47
'connection ' => $ event ->connectionName ,
47
48
'database ' => $ this ->config ->get ("database.connections. {$ event ->connectionName }.database " ),
49
+ 'callSpot ' => $ this ->guessCallSpot (),
48
50
]);
49
51
}
50
52
@@ -106,4 +108,18 @@ protected function quote(?\PDO $pdo, string $value): string
106
108
107
109
return "' " . \str_replace ($ search , $ replace , $ value ) . "' " ;
108
110
}
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
+ }
109
125
}
0 commit comments