mortenscheel/laravel-query-recorder
Composer 安装命令:
composer require mortenscheel/laravel-query-recorder
包简介
A package to record and analyze database queries in Laravel applications.
README 文档
README
Laravel Query Recorder
A package to record and analyze database queries in Laravel applications.
Installation
You can install the package via composer:
composer require mortenscheel/laravel-query-recorder --dev
Usage
This package comes with a couple of recorders.
Recording all queries to CSV
The package includes a CSV recorder that can write all queries to a CSV file. Example use in a controller:
use Scheel\QueryRecorder\Facades\QueryRecorder; use Scheel\QueryRecorder\Processors\CsvProcessor; class UserController extends Controller { public function index() { // Create a CSV recorder with a file path $recorder = new CsvProcessor(storage_path('queries.csv')); // Start recording QueryRecorder::record($recorder); // Your controller logic with database queries $users = User::all(); // The CSV will be written after the response has been sent // thanks to Laravel's defer() mechanism return view('users.index', compact('users')); } }
The CSV file will include:
- Query time (ms)
- Origin (file and line number)
- SQL query (raw)
Record duplicate queries to CSV
use Scheel\QueryRecorder\Processors\DuplicateQueryCsvProcessor; QueryRecorder::record(new DuplicateQueryCsvProcessor('/path/to/output.csv'));
This recorder will only show duplicate queries, grouped by both sql and origin. There will be one row per unique sql+origin, containing:
- Count
- Total time (ms)
- Origin
- SQL
Custom Recorders
You can create your own custom recorder by implementing the RecordsQueries interface:
use Scheel\QueryRecorder\RecordsQueries; use Scheel\QueryRecorder\QueryCollection; class CustomProcessor implements QueryCollectionProcessor { public function process(QueryCollection $queries): void { // Custom implementation to record the queries collection // This will be called after the request is complete } }
Usage example:
$recorder = new CustomProcessor(); QueryRecorder::record($recorder); // Execute queries... DB::table('users')->first(); // The recordQueries method will be called after the response // has been sent, with all collected queries
Query Origin Tracking
One of the key features of this package is the ability to identify where in your code a query is being executed:
$query->origin->file // The file path where the query was initiated $query->origin->line // The line number where the query was initiated $query->origin->function // The function that initiated the query $query->origin->class // The class that initiated the query (if applicable) $query->origin->type // The type of call (static or instance method) // Additional helper methods $query->origin->isVendor() // Check if the query originated from a vendor package $query->origin->location() // Get the file:line format $query->origin->editorLink() // Get an editor link to the exact location
This is especially helpful for debugging and optimizing database queries in your application.
Listening for Queries
This is similar to Laravel's DB::listen() except you receive a RecordedQuery with extra metadata.
use Scheel\QueryRecorder\Facades\QueryRecorder; QueryRecorder::listen(function (RecordedQuery $query) { // Do something with the recorded query Log::debug('Query executed', ['origin' => $query->location(), 'sql' => $query->sql]); }); // Execute database queries... DB::table('users')->first();
Testing
composer test
Security
If you discover any security related issues, please email the author instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
mortenscheel/laravel-query-recorder 适用场景与选型建议
mortenscheel/laravel-query-recorder 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 10.49k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「package」 「php」 「Skeleton」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mortenscheel/laravel-query-recorder 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mortenscheel/laravel-query-recorder 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mortenscheel/laravel-query-recorder 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Simple ASCII output of array data
Slim starter / Slim skeleton package to boost your development with Slim framework
Base Skeleton for Laravel Application
An awesome skeleton for modern PHP development.
Alfabank REST API integration
统计信息
- 总下载量: 10.49k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-03-13