Drupal is a registered trademark of Dries Buytaert

mysql_query_logger is a minimal module that logs all Drupal database activity to a custom file. You do not need to install or enable the module via the Drupal UI—simply add the code via Composer and update your database configuration.

Features

This module provides a custom MySQL driver for Drupal 10, which extends the standard driver and logs every database operation (SELECT, INSERT, UPDATE, DELETE, MERGE, etc.).

Each log entry includes:

  • Timestamp
  • Operation type
  • Execution time
  • Query details (JSON formatted for easy analysis)

Example log line:

[2024-06-27 16:38:15] INSERT (0.003521 s): ["users",{"fields":{"name":"testuser"}}]

Installation

1. Install via Composer

It's recommended to add the module to your project using Composer. If not already distributed via a Composer repository, you can use path repositories.

composer config repositories.mysql_query_logger path 'modules/custom/mysql_query_logger'
composer require drupal/mysql_query_logger:*

Or, if already published to a VCS or Packagist:

composer require drupal/mysql_query_logger

Note: Adapt the path if your module resides elsewhere.

2. Update Database Settings

Open settings.php and adjust your database configuration as follows (usually at the end of the file):

// Settings to record database activity.
$databases['default']['default']['driver'] = 'mysql_query_logger';
$databases['default']['default']['namespace'] = 'Drupal\mysql_query_logger\Driver\Database\mysql_query_logger';
$databases['default']['default']['autoload'] = 'web/modules/custom/mysql_query_logger/src/Driver/Database/mysql_query_logger/Connection.php';
// Set an absolute or relative path for your log output file.
$databases['default']['default']['output'] = '/absolute/path/mysql_query_logger.log';

require_once DRUPAL_ROOT . '/core/modules/mysql/src/Driver/Database/mysql/Connection.php';
require_once DRUPAL_ROOT . '/modules/custom/mysql_query_logger/src/Driver/Database/mysql_query_logger/Connection.php';

TIP: Adjust the autoload and output paths to match your project structure.

3. Permissions

Ensure that PHP has write access to the log file location. For example:

touch /absolute/path/mysql_query_logger.log
chmod 664 /absolute/path/mysql_query_logger.log
chown www-data:www-data /absolute/path/mysql_query_logger.log

Removal

To disable the logger, simply remove or comment out the custom database settings in your settings.php. You can then remove the module via Composer if desired.

Security & Usage Notes

  • Production caution: Do not use this module in production without considering log size and sensitivity.
  • Log files can grow quickly on high-traffic sites.
  • Logs may contain sensitive queries (e.g., tokens, hashed passwords).

Support

This module is offered “as-is” and is intended primarily for development, debugging, and auditing scenarios.

Alternatives

  • Use Symfony query logger or Xdebug for PHP.
  • Consider the Drupal Database Logging or Query Monitor modules.
  • Enable the MySQL Slow Query Log at the database server level.

Note:

When using this module as your database driver, certain configuration export/import operations and other Drush utilities may stop working or behave unexpectedly. This is because some Drupal and Drush commands depend on the default database driver or expect specific behaviors not implemented in this custom driver. For critical Drush tasks, we recommend temporarily reverting to the standard MySQL driver.

Activity

Total releases
2
First release
Jul 2025
Latest release
7 months ago
Release cadence
0 days
Stability
50% stable

Releases

Version Type Release date
1.0.0 Stable Jul 8, 2025
1.0.x-dev Dev Jul 8, 2025