hesamrad / laravel-sql-logger
A lightweight package to log SQL queries in your Laravel application
Installs: 1
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
pkg:composer/hesamrad/laravel-sql-logger
Requires
- php: ^8.1
- illuminate/config: ^10.0|^11.0|^12.0
- illuminate/database: ^10.0|^11.0|^12.0
- illuminate/support: ^10.0|^11.0|^12.0
- psr/log: ^3.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0
README
A lightweight package to log SQL queries in your Laravel application; perfect for debugging and performance analysis during development.
Installation
Install via Composer:
composer require hesamrad/laravel-sql-logger --dev
The package will automatically register its service provider.
Configuration
Publish the configuration file:
php artisan vendor:publish --provider="HesamRad\LaravelSqlLogger\LaravelSqlLoggerServiceProvider"
This will create config/sql-logger.php config file.
Usage
Once installed and configured, SQL logging will automatically start based on your configuration.
Viewing Logs
SQL queries are logged to storage/logs/sql.log by default. You can view them with:
# Tail the log file (Linux/Mac) tail -f storage/logs/sql.log # View entire log cat storage/logs/sql.log
Example Output
[2024-01-15 10:30:45] [2.5ms] select * from `users` where `email` = ? ["user@example.com"]
[2024-01-15 10:30:45] [1.2ms] select * from `posts` where `user_id` = ? [1]
[2024-01-15 10:30:45] [0.8ms] select * from `comments` where `post_id` in (?, ?, ?) [1, 2, 3]
------------------------------------------------------------
[2024-01-15 10:31:12] [3.1ms] insert into `users` (`name`, `email`) values (?, ?) ["John Doe", "john@example.com"]
[2024-01-15 10:31:12] [1.5ms] select * from `users` where `id` = ? [42]
------------------------------------------------------------
Troubleshooting
Log file not created
- Check
storage/logsdirectory permissions:chmod 775 storage/logs chown www-data:www-data storage/logs
- Verify
APP_DEBUG=truein your.env - Check current environment is in
allowed_envs - Ensure not running in console mode
Empty or incomplete logs
- Try disabling file locking:
'file_lock' => false, - Check disk space
- Verify PHP has write permissions
Performance issues
- Set
file_lock' => false - Only enable in specific environments
- Monitor log file size
Multiple separator lines
This indicates the service provider is being instantiated multiple times. Ensure:
- You're using the latest version
static $attachedproperty is working correctly- Only one instance of the provider exists
Security Considerations
⚠️ Important Security Notes:
- Never enable in production unless absolutely necessary
- SQL logs may contain sensitive data (emails, passwords, personal info)
- Secure the log file:
chmod 640 storage/logs/sql.log chown www-data:www-data storage/logs/sql.log
- Regularly rotate and delete old logs
Support
If you encounter any issues or have questions:
- Check the Troubleshooting section
- Search existing issues
- Create a new issue
License
This package is open-source software licensed under the MIT license.