eta/laravel-query-watcher
Composer 安装命令:
composer require eta/laravel-query-watcher
包简介
A Laravel package to monitor and log database queries with configurable thresholds
README 文档
README
A Laravel package to monitor and log database queries with configurable thresholds and detailed performance metrics.
Features
- 🔍 Query Count Monitoring: Track the number of queries executed per request/command
- ⚡ Slow Query Detection: Identify queries that exceed execution time thresholds
- 🎯 Flexible Configuration: Customize thresholds, log levels, and channels
- 🚫 Path/Command Exclusions: Exclude specific routes or artisan commands
- 🌍 Environment-Specific: Enable only in desired environments
- 📊 Detailed Logging: Optional query SQL and bindings in logs
Installation
Install the package via Composer:
composer require eta/laravel-query-watcher
Laravel Auto-Discovery
The package will automatically register its service provider in Laravel 5.5+.
Manual Registration (Laravel 5.4 and below)
Add the service provider to config/app.php:
'providers' => [ // ... ETA\LaravelQueryWatcher\QueryWatcherServiceProvider::class, ],
Configuration
Publish the configuration file:
php artisan vendor:publish --tag=query-watcher-config
This will create config/query-watcher.php with the following options:
Main Configuration Options
return [ // Enable/disable the watcher 'enabled' => env('QUERY_WATCHER_ENABLED', true), // Query count threshold 'threshold' => env('QUERY_WATCHER_THRESHOLD', 50), // Log level (emergency, alert, critical, error, warning, notice, info, debug) 'log_level' => env('QUERY_WATCHER_LOG_LEVEL', 'warning'), // Log channel (null for default) 'log_channel' => env('QUERY_WATCHER_LOG_CHANNEL', null), // Slow query threshold in milliseconds (null to disable) 'slow_query_threshold' => env('QUERY_WATCHER_SLOW_QUERY_THRESHOLD', 1000), // Log query SQL and bindings (be careful with sensitive data) 'log_query_details' => env('QUERY_WATCHER_LOG_DETAILS', false), // Excluded paths (supports wildcards) 'excluded_paths' => [ // 'admin/*', // '_debugbar/*', ], // Excluded artisan commands 'excluded_commands' => [ // 'migrate', // 'db:seed', ], ];
QUERY_WATCHER_SLOW_QUERY_ENABLED=false
Slow query threshold (milliseconds)
QUERY_WATCHER_SLOW_QUERY_THRESHOLD=1000
Log query details (SQL and bindings)
QUERY_WATCHER_LOG_DETAILS=false
Custom log channel
QUERY_WATCHER_LOG_CHANNEL=stack
## Usage
Once installed and configured, the package works automatically. It will:
1. **Monitor all database queries** on each request or artisan command
2. **Log warnings** when query count exceeds the threshold
3. **Detect slow queries** that exceed the execution time threshold
4. **Respect exclusions** for specific paths or commands
### Example Log Output
**High Query Count Warning:**
[2024-01-15 10:30:45] local.WARNING: [QueryWatcher] High number of DB queries detected on api/users: 75 queries (threshold: 50)
{
"path": "api/users",
"query_count": 75,
"threshold": 50,
"slow_queries_count": 3
}
**Slow Query Warning:**
[2024-01-15 10:30:45] local.WARNING: [QueryWatcher] Slow query detected on api/users: 1250.50ms
{
"path": "api/users",
"execution_time_ms": 1250.50
}
## Advanced Usage
### Using a Custom Log Channel
Create a custom log channel in `config/logging.php`:
```php
'channels' => [
'query-watcher' => [
'driver' => 'single',
'path' => storage_path('logs/query-watcher.log'),
'level' => 'warning',
],
],
Then update your .env:
QUERY_WATCHER_LOG_CHANNEL=query-watcher
Excluding Specific Paths
In config/query-watcher.php:
'excluded_paths' => [ 'admin/*', '_debugbar/*', 'api/webhooks/*', ],
Excluding Artisan Commands
In config/query-watcher.php:
'excluded_commands' => [ 'migrate', 'migrate:*', 'db:seed', 'queue:work', ],
Best Practices
- Sensitive Data: Be cautious when enabling
log_query_detailsas it may expose sensitive data in logs - Production: Consider disabling in production by setting
QUERY_WATCHER_ENABLED=falseor using a separate log channel - Thresholds: Adjust thresholds based on your application's normal query patterns
- Exclusions: Exclude admin panels, debug tools, and high-traffic endpoints that you're aware of
Requirements
- PHP 7.2 or higher
- Laravel 5.8, 6.x, 7.x, 8.x, 9.x, 10.x, or 11.x
License
The MIT License (MIT). Please see License File for more information.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Credits
Support
If you discover any security-related issues, please email support@ethan-tech.com instead of using the issue tracker.
eta/laravel-query-watcher 适用场景与选型建议
eta/laravel-query-watcher 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 83 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「database」 「performance」 「query」 「monitoring」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 eta/laravel-query-watcher 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eta/laravel-query-watcher 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 eta/laravel-query-watcher 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
g4 application profiler package
CSS/Javascript Minificator, Compressor and Concatenator for TYPO3 - highly configurable frontend asset optimization for CSS/JS merging, minification and compression with optional body parsing, async/defer loading, inline output, data-ignore exclusions, SRI integrity validation/calculation, external
WordPress mu-plugin to remove jQuery Migrate from the list of jQuery dependencies and to allow jQuery to enqueue before </body> instead of in the <head>.
Store your language lines in the database, yaml or other sources
Create link to static resources with cache-breaking segment based on md5 of the file
统计信息
- 总下载量: 83
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-26