dottedai/laravel-query-logger
Composer 安装命令:
composer require dottedai/laravel-query-logger
包简介
A package to log and display database queries like Rails console.
README 文档
README
A simple Laravel package to log and display database queries similar to Rails' console output. This tool is intended for use in local development environments, making it easier to debug and optimize your SQL queries.
Features
- Automatic Query Logging: Listens to all database queries executed by your Laravel application.
- Log File Integration: Records each query, including execution time, into Laravel's default log file (
storage/logs/laravel.log). - Browser Console Output: Optionally outputs queries to the browser's JavaScript console for quick inspection.
- Route Logging: Logs route details—including the URI, controller, and middleware—to provide a complete view of the request lifecycle.
- Console Info Logging: Displays
Log::info(and higher level) messages in the terminal (when usingphp artisan serve) by adding a custom Monolog handler. - Development Focused: Designed to work in the local environment to aid debugging without impacting production performance.
Requirements
- PHP >= 7.2
- Laravel 6.x, 7.x, 8.x, 9.x, or 10.x
Installation
You can install the package via Composer:
composer require dottedai/laravel-query-logger
Laravel's package auto-discovery will automatically register the service provider. If you need to manually register it, add the following to your config/app.php file within the providers array:
Dottedai\QueryLogger\QueryLoggerServiceProvider::class,
Usage
Once installed and registered, the package will automatically begin listening for database queries, route events, and log messages when your application is running in the local environment.
How It Works
Query Listener
The package uses Laravel's DB::listen method to capture all SQL queries, replaces query placeholders with their actual binding values, and logs the query along with its execution time.
Route Logging
It listens for Laravel's RouteMatched event to log route details such as the URI, controller handling the request, and any associated middleware.
Console Output
When using the built-in PHP server (php artisan serve), queries and route logs are output to the terminal using PHP's error_log() function.
Console Info Logging
A custom Monolog handler is added to also output Log::info (and higher-level) messages to the terminal. This means that any log calls such as Log::info, Log::warning, or Log::error will appear in your console when running locally.
Example Output
When a query is executed and a route is matched, you might see output similar to this in your log file and terminal:
Query: SELECT * FROM users WHERE email = 'example@example.com' [12 ms]
Route: api/users | Controller: App\Http\Controllers\UserController@index | Middleware: api, auth
INFO: Application started successfully.
Customization
You can modify the behavior of the query logger by editing the QueryLoggerServiceProvider class. For example, you might choose to disable the JavaScript console output, adjust the logging level, or customize the route logging format to fit your needs.
Important Considerations
Development Use Only
This package is meant for local development environments. Ensure it is disabled or removed from production to prevent potential performance issues or the inadvertent exposure of sensitive query data.
Security
Logging SQL queries and other sensitive application data can expose critical information. Always use this tool only in safe, non-production environments.
Contributing
Contributions, suggestions, and bug reports are welcome! Feel free to submit an issue or pull request on GitHub.
License
This package is open-sourced software licensed under the MIT license.
dottedai/laravel-query-logger 适用场景与选型建议
dottedai/laravel-query-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.22k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 dottedai/laravel-query-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 dottedai/laravel-query-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.22k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Unknown
- 更新时间: 2025-03-12