@@ -48,7 +48,7 @@ class Connection extends BaseConnection
48
48
*/
49
49
protected $ connection ;
50
50
51
- private ?CommandSubscriber $ commandSubscriber ;
51
+ private ?CommandSubscriber $ commandSubscriber = null ;
52
52
53
53
/**
54
54
* Create a new database connection instance.
@@ -65,8 +65,6 @@ public function __construct(array $config)
65
65
66
66
// Create the connection
67
67
$ this ->connection = $ this ->createConnection ($ dsn , $ config , $ options );
68
- $ this ->commandSubscriber = new CommandSubscriber ($ this );
69
- $ this ->connection ->addSubscriber ($ this ->commandSubscriber );
70
68
71
69
// Select database
72
70
$ this ->db = $ this ->connection ->selectDatabase ($ this ->getDefaultDatabaseName ($ dsn , $ config ));
@@ -141,6 +139,36 @@ public function getDatabaseName()
141
139
return $ this ->getMongoDB ()->getDatabaseName ();
142
140
}
143
141
142
+ public function enableQueryLog ()
143
+ {
144
+ parent ::enableQueryLog ();
145
+
146
+ $ this ->commandSubscriber = new CommandSubscriber ($ this );
147
+ $ this ->connection ->addSubscriber ($ this ->commandSubscriber );
148
+ }
149
+
150
+ public function disableQueryLog ()
151
+ {
152
+ parent ::disableQueryLog (); // TODO: Change the autogenerated stub
153
+
154
+ $ this ->connection ->removeSubscriber ($ this ->commandSubscriber );
155
+ $ this ->commandSubscriber = null ;
156
+ }
157
+
158
+ protected function withFreshQueryLog ($ callback )
159
+ {
160
+ try {
161
+ return parent ::withFreshQueryLog ($ callback );
162
+ } finally {
163
+ // The parent method enable query log using enableQueryLog()
164
+ // but disables it by setting $loggingQueries to false. We need to
165
+ // remove the subscriber for performance.
166
+ if (! $ this ->loggingQueries ) {
167
+ $ this ->disableQueryLog ();
168
+ }
169
+ }
170
+ }
171
+
144
172
/**
145
173
* Get the name of the default database based on db config or try to detect it from dsn.
146
174
*
0 commit comments