laravel-db-query-sniffer maintained by andriichuk
Description
Laravel package to log SQL queries and Redis commands with raw SQL, binary-safe bindings, and slow-query filters
Author
Last update
2026/07/22 10:03
(dev-main)
License
Downloads
1
Tags
Laravel DB Query Sniffer
Log SQL queries and Redis commands in Laravel during local development — with raw SQL, binary-safe binding fallbacks, and optional slow-query filters.
Requirements
- PHP 8.3+
- Laravel 11.x, 12.x, or 13.x
Installation
composer require andriichuk/laravel-db-query-sniffer --dev
Publish the config (optional):
php artisan vendor:publish --tag="db-query-sniffer-config"
Add dedicated log channels in config/logging.php (recommended):
'channels' => [
// ...
'db' => [
'driver' => 'daily',
'path' => storage_path('logs/db.log'),
'level' => 'debug',
'days' => 2,
],
'redis' => [
'driver' => 'daily',
'path' => storage_path('logs/redis.log'),
'level' => 'debug',
'days' => 2,
],
],
When is logging on? If
LOG_DB_QUERIESis not set, the sniffer followsAPP_DEBUG. SetLOG_DB_QUERIES=trueorfalseto force it. Override channels withDB_QUERY_LOG_CHANNELandREDIS_COMMAND_LOG_CHANNEL.
Configuration
| Key | Description | Default |
|---|---|---|
enabled |
Master switch | env('LOG_DB_QUERIES', APP_DEBUG) |
environments |
Environments where listeners are registered (['*'] = all) |
['local'] |
database.enabled |
Log SQL queries | true |
database.channel |
Log channel for SQL | db |
database.slow_only |
When true, only log queries ≥ slow_ms |
false |
database.slow_ms |
Minimum query duration (ms) when slow_only is enabled |
100 |
database.include_connection |
Include connection name in context | true |
redis.enabled |
Log Redis commands | true |
redis.channel |
Log channel for Redis | redis |
redis.slow_only |
When true, only log commands ≥ slow_ms |
false |
redis.slow_ms |
Minimum command duration (ms) when slow_only is enabled |
100 |
bindings.max_string_length |
Truncate string bindings (null = no limit) |
null |
bindings.binary_as_base64 |
Include base64 for binary bindings | true |
Example log output
[2026-07-22 06:50:01] local.INFO: SQL Query: {"query":"select * from `users` where `id` = 1","time":"1.24ms","connection":"mysql"}
[2026-07-22 06:50:01] local.INFO: Redis Command: {"command":"GET","parameters":["user:1"],"time":"0.31ms","connection":"default"}
When toRawSql() fails (e.g. binary bindings), a warning is logged with sanitized bindings instead of crashing:
[2026-07-22 06:50:02] local.WARNING: Failed to build raw SQL query string. Logging fallback payload. {"error":"...","sql":"select ?","bindings":["[binary:16 bytes;base64=...]"],"time":"0.5ms","connection":"mysql"}
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
License
The MIT License (MIT). Please see License File for more information.