andriichuk / laravel-db-query-sniffer
Laravel package to log SQL queries and Redis commands with raw SQL, binary-safe bindings, and slow-query filters
Package info
github.com/andriichuk/laravel-db-query-sniffer
pkg:composer/andriichuk/laravel-db-query-sniffer
Requires
- php: ^8.3
- illuminate/database: ^11.0||^12.0||^13.0
- illuminate/redis: ^11.0||^12.0||^13.0
- illuminate/support: ^11.0||^12.0||^13.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.9
- orchestra/testbench: ^11.0.0||^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
README
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.