Now an event can be dispatched at the start and at the end of a query statement execution. This can allow profilers to collect and log database query starts and ends. This can then be combined with other logged data to better analyse and debug Drupal.
The events are not dispatched by default, to avoid performance impacts. Calling code needs to opt-in for event dispatching by calling ::enableEvents(TRUE) on the database Connection object. The legacy database logger does so when logging database queries is enabled by running Drupal\Core\Database\Database::startLog('YOUR_LOGGING_KEY').
Both new events implement the class Drupal\Core\Database\Event\StatementExecutionEvent.
API additions:
-
The method
Drupal\Core\Database\Log::logFromEvent()adds database query loggings events data to the query log. -
The method
Drupal\Core\Database\Connection::isEventEnabled()returns the status of the database API events being dispatched. -
The methods
Drupal\Core\Database\Connection::enableEvents()andDrupal\Core\Database\Connection::disableEvents()enable or disable the dispatching of database API events. -
The method
Drupal\Core\Database\Connection::dispatchEvent()is used to throw database API events. -
The method
Drupal\Core\Database\Connection::findCallerFromDebugBacktrace()is used get the last non-database call from the debug backtrace data. -
The method
Drupal\Core\Database\Connection::removeDatabaseEntriesFromDebugBacktrace()is used remove database calls from the debug backtrace data.
API deprecations:
-
The method
Drupal\Core\Database\Log::log()and is replaced by the new methodDrupal\Core\Database\Log::logFromEvent(). -
The method
Drupal\Core\Database\Log::findCaller()and is replaced by the new methodDrupal\Core\Database\Connection::findCallerFromDebugBacktrace() -
The method
Drupal\Core\Database\Log::removeDatabaseEntries()and is replaced by the new methodDrupal\Core\Database\Connection::removeDatabaseEntriesFromDebugBacktrace() -
The method
Drupal\Core\Database\Log::getDebugBacktrace()and there is no replacement.
Comments
StatementExecutionFailureEvent
in 10.3 there was added handling of exceptions in queries