Drupal is a registered trademark of Dries Buytaert
Protected Pages 3.0.0 Major update available for module protected_pages (3.0.0). Commerce Core 3.3.8 Minor update available for module commerce (3.3.8). Layout Builder Reorder 2.0.1 Minor update available for module layout_builder_reorder (2.0.1). Ban 1.1.0 Minor update available for module ban (1.1.0). Field Formatter Range 2.0.0 Major update available for module field_formatter_range (2.0.0). Field Formatter Range 8.x-1.8 Minor update available for module field_formatter_range (8.x-1.8). UI Patterns (SDC in Drupal UI) 2.0.18 Minor update available for module ui_patterns (2.0.18). Varbase FAQs 9.2.1 Minor update available for module varbase_faqs (9.2.1). Search API Solr 4.3.11 Module search_api_solr updated after 14 months of inactivity (4.3.11). Provus Mega Menu Module provus_mega_menu crossed 1,000 active installs.

This module logs all database queries made by Drupal to a custom file. It records the timestamp, operation type, execution time, and the query itself for each database activity.

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
1 year ago
Releases (12 mo)
0 ▼ from 2
Maintenance
Dormant

Releases

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