1414
1515namespace RodrigoPedra \QueryLogger ;
1616
17+ use Illuminate \Contracts \Config \Repository ;
1718use Illuminate \Database \Events \QueryExecuted ;
19+ use Illuminate \Support \Arr ;
1820use Psr \Log \LoggerInterface ;
1921
2022class QueryLogger
2123{
2224 protected LoggerInterface $ logger ;
25+ protected Repository $ config ;
2326
24- public function __construct (LoggerInterface $ logger )
27+ public function __construct (LoggerInterface $ logger, Repository $ config )
2528 {
2629 $ this ->logger = $ logger ;
30+ $ this ->config = $ config ;
2731 }
2832
29- public function handle (QueryExecuted $ event )
33+ public function handle (QueryExecuted $ event ): void
3034 {
3135 $ pdo = \method_exists ($ event ->connection , 'getPdo ' )
3236 ? $ event ->connection ->getPdo ()
@@ -41,6 +45,8 @@ public function handle(QueryExecuted $event)
4145 'bindings ' => $ event ->bindings ,
4246 'time ' => $ event ->time ,
4347 'connection ' => $ event ->connectionName ,
48+ 'database ' => $ this ->config ->get ("database.connections. {$ event ->connectionName }.database " ),
49+ 'callSpot ' => $ this ->guessCallSpot (),
4450 ]);
4551 }
4652
@@ -57,7 +63,7 @@ protected function prepareQuery(string $query, array $bindings): string
5763 return $ query ;
5864 }
5965
60- protected function prepareValue ($ pdo , $ value ): string
66+ protected function prepareValue (? \ PDO $ pdo , $ value ): string
6167 {
6268 if (\is_null ($ value )) {
6369 return 'NULL ' ;
@@ -75,23 +81,23 @@ protected function prepareValue($pdo, $value): string
7581 return $ this ->quote ($ pdo , '[BINARY DATA] ' );
7682 }
7783
78- if (\is_object ($ value ) && \method_exists ($ value , '__toString ' )) {
79- $ value = \strval ($ value );
80- }
81-
8284 if (\is_object ($ value ) && \method_exists ($ value , 'toString ' )) {
8385 $ value = $ value ->toString ();
8486 }
8587
86- if (\is_object ( $ value) && \is_a ( $ value , \ DateTimeInterface::class) ) {
88+ if ($ value instanceof \ DateTimeInterface) {
8789 $ value = $ value ->format ('Y-m-d H:i:s ' );
8890 }
8991
92+ if (\is_object ($ value ) && \method_exists ($ value , '__toString ' )) {
93+ $ value = \strval ($ value );
94+ }
95+
9096 // objects not implementing __toString() or toString() will fail here
9197 return $ this ->quote ($ pdo , \strval ($ value ));
9298 }
9399
94- protected function quote ($ pdo , string $ value ): string
100+ protected function quote (? \ PDO $ pdo , string $ value ): string
95101 {
96102 if ($ pdo ) {
97103 return $ pdo ->quote ($ value );
@@ -102,4 +108,18 @@ protected function quote($pdo, string $value): string
102108
103109 return "' " . \str_replace ($ search , $ replace , $ value ) . "' " ;
104110 }
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 (\array_key_exists ('file ' , $ trace ) && ! \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+ }
105125}
0 commit comments