smurrlawa / laravel-query-logger
A Laravel package to log all database queries to a configurable log channel or handler.
Requires
- php: ^8.2
- illuminate/database: ^11.0|^12.0
- illuminate/log: ^11.0|^12.0
- illuminate/support: ^11.0|^12.0
Requires (Dev)
- orchestra/testbench: ^10.4
- phpunit/phpunit: ^12.2
README
A simple Laravel package to log all database queries to a configurable log channel. Supports conditional logging by environment and slow query reporting.
π¦ Installation
composer require smurrlawa/laravel-query-logger
βοΈ Configuration
Publish the config file
php artisan vendor:publish --tag=query-logger-config
This will publish the configuration to config/query-logger.php
.
Available Options
return [ 'enabled' => env('QUERY_LOGGER_ENABLED', true), 'channel' => env('QUERY_LOGGER_CHANNEL', 'daily'), 'slow_queries_enabled' => env('QUERY_LOGGER_SLOW_QUERIES_ENABLED', true), 'slow_queries_threshold' => env('QUERY_LOGGER_SLOW_QUERIES_THRESHOLD', 1000), 'environments' => ['local', 'staging'], ];
β Option Descriptions
Option | Description |
---|---|
enabled |
Enable or disable query logging completely. |
channel |
The log channel to use (config/logging.php or PSR logger class). |
slow_queries_enabled |
Whether to log slow queries separately. |
slow_queries_threshold |
Time in milliseconds to consider a query as slow. |
environments |
List of environments where query logging should be active. |
π§ͺ Example Usage
To log all queries in local
environment and warn about slow queries:
APP_ENV=local QUERY_LOGGER_ENABLED=true QUERY_LOGGER_CHANNEL=daily QUERY_LOGGER_SLOW_QUERIES_ENABLED=true QUERY_LOGGER_SLOW_QUERIES_THRESHOLD=500
π Features
π Logs all database queries (SQL, bindings, execution time)
π’ Logs slow queries exceeding a customizable threshold
βοΈ Fully configurable via config file and environment variables
π Supports environment-specific logging (e.g., local, staging)
π‘ Supports custom log channels and PSR-3 loggers
β Compatibility
Tested with Laravel 11 and Laravel 12.
π License
MIT Β© Smurrlawa