14
14
15
15
namespace RodrigoPedra \QueryLogger ;
16
16
17
+ use Illuminate \Contracts \Config \Repository ;
17
18
use Illuminate \Database \Events \QueryExecuted ;
19
+ use Illuminate \Support \Arr ;
18
20
use Psr \Log \LoggerInterface ;
19
21
20
22
class QueryLogger
21
23
{
22
24
protected LoggerInterface $ logger ;
25
+ protected Repository $ config ;
23
26
24
- public function __construct (LoggerInterface $ logger )
27
+ public function __construct (LoggerInterface $ logger, Repository $ config )
25
28
{
26
29
$ this ->logger = $ logger ;
30
+ $ this ->config = $ config ;
27
31
}
28
32
29
- public function handle (QueryExecuted $ event )
33
+ public function handle (QueryExecuted $ event ): void
30
34
{
31
35
$ pdo = \method_exists ($ event ->connection , 'getPdo ' )
32
36
? $ event ->connection ->getPdo ()
@@ -41,6 +45,8 @@ public function handle(QueryExecuted $event)
41
45
'bindings ' => $ event ->bindings ,
42
46
'time ' => $ event ->time ,
43
47
'connection ' => $ event ->connectionName ,
48
+ 'database ' => $ this ->config ->get ("database.connections. {$ event ->connectionName }.database " ),
49
+ 'callSpot ' => $ this ->guessCallSpot (),
44
50
]);
45
51
}
46
52
@@ -57,7 +63,7 @@ protected function prepareQuery(string $query, array $bindings): string
57
63
return $ query ;
58
64
}
59
65
60
- protected function prepareValue ($ pdo , $ value ): string
66
+ protected function prepareValue (? \ PDO $ pdo , $ value ): string
61
67
{
62
68
if (\is_null ($ value )) {
63
69
return 'NULL ' ;
@@ -75,23 +81,23 @@ protected function prepareValue($pdo, $value): string
75
81
return $ this ->quote ($ pdo , '[BINARY DATA] ' );
76
82
}
77
83
78
- if (\is_object ($ value ) && \method_exists ($ value , '__toString ' )) {
79
- $ value = \strval ($ value );
80
- }
81
-
82
84
if (\is_object ($ value ) && \method_exists ($ value , 'toString ' )) {
83
85
$ value = $ value ->toString ();
84
86
}
85
87
86
- if (\is_object ( $ value) && \is_a ( $ value , \ DateTimeInterface::class) ) {
88
+ if ($ value instanceof \ DateTimeInterface) {
87
89
$ value = $ value ->format ('Y-m-d H:i:s ' );
88
90
}
89
91
92
+ if (\is_object ($ value ) && \method_exists ($ value , '__toString ' )) {
93
+ $ value = \strval ($ value );
94
+ }
95
+
90
96
// objects not implementing __toString() or toString() will fail here
91
97
return $ this ->quote ($ pdo , \strval ($ value ));
92
98
}
93
99
94
- protected function quote ($ pdo , string $ value ): string
100
+ protected function quote (? \ PDO $ pdo , string $ value ): string
95
101
{
96
102
if ($ pdo ) {
97
103
return $ pdo ->quote ($ value );
@@ -102,4 +108,18 @@ protected function quote($pdo, string $value): string
102
108
103
109
return "' " . \str_replace ($ search , $ replace , $ value ) . "' " ;
104
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 (\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
+ }
105
125
}
0 commit comments