简单到无法无天 / laravel-performance-log
Laravel用于记录应用程序慢速部分的日志服务。
Requires
- php: ^8.1
- illuminate/console: ^8.0|^9.0|^10.0|^11.0
- illuminate/contracts: ^8.0|^9.0|^10.0|^11.0
- illuminate/database: ^8.0|^9.0|^10.0|^11.0
- illuminate/events: ^8.0|^9.0|^10.0|^11.0
- illuminate/http: ^8.0|^9.0|^10.0|^11.0
- illuminate/log: ^8.0|^9.0|^10.0|^11.0
- illuminate/queue: ^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
- simple-as-fuck/php-validator: ^0.1.0|^0.2.0|^0.3.0|^0.4.0|^0.5.0|^0.6.0
Requires (Dev)
Suggests
- sentry/sentry-laravel: Allows receive logs with performance warning and make statistic on them, send notification, ...
README
Laravel用于记录应用程序慢速部分的日志服务。
安装
composer require simple-as-fuck/laravel-performance-log
配置
php artisan vendor:publish --tag performance-log-config
支持
如果composer.json中的任何PHP平台需求以安全支持结束,则将包版本视为除最后一个版本之外的所有版本均不受支持。
Http中间件使用
对于http请求时间日志,您必须注册PerformanceMiddleware
。查看laravel文档了解如何使用laravel中间件。
推荐用法是将中间件作为全局中间件在第一个位置注册,并测量所有请求。
如果想在路由组上注册中间件,必须配置中间件优先级并将PerformanceMiddleware
放在第一个位置。
阈值覆盖
Sql
如果您知道某些SQL查询很慢,并且您对此可以接受,可以通过在PerformanceLogConfig
中设置临时阈值来覆盖'performance_log.database.slow_query_threshold'
或'performance_log.database.slow_transaction_threshold'
。
/** @var \SimpleAsFuck\LaravelPerformanceLog\Service\PerformanceLogConfig $config */ $config = app()->make(\SimpleAsFuck\LaravelPerformanceLog\Service\PerformanceLogConfig::class); $sqlThreshold = $config->setSlowSqlQueryThreshold(null); $transactionThreshold = $config->setSlowDbTransactionThreshold(null); // run some slow queries without annoying performance log $sqlThreshold->restore(); $transactionThreshold->restore();
Http
如果您知道某些具体的控制器操作很慢或应该非常快,可以通过设置临时阈值来覆盖'performance_log.http.slow_request_threshold'
。临时阈值可以在每个请求中仅设置一次,并在请求结束时保持有效。
/** @var \SimpleAsFuck\LaravelPerformanceLog\Service\PerformanceLogConfig $config */ $config = app()->make(\SimpleAsFuck\LaravelPerformanceLog\Service\PerformanceLogConfig::class); $config->setSlowRequestThreshold(null); // run some extra slow logic without annoying performance log // no need for threshold restoring, performance middleware will handle it
控制台
如果您想覆盖'performance_log.console.slow_command_threshold'
,可以通过设置临时阈值来实现。临时阈值可以在每个命令运行中仅设置一次,并在命令结束时保持有效。
/** @var \SimpleAsFuck\LaravelPerformanceLog\Service\PerformanceLogConfig $config */ $config = app()->make(\SimpleAsFuck\LaravelPerformanceLog\Service\PerformanceLogConfig::class); $config->setSlowCommandThreshold(60); // one minute // no need for threshold restoring, performance listener will handle it
作业
如果您想覆盖'performance_log.queue.slow_job_threshold'
,可以在作业运行中的任何位置设置临时阈值。临时阈值可以设置在作业运行中的任何位置,并在作业结束时保持有效。
/** @var \SimpleAsFuck\LaravelPerformanceLog\Service\PerformanceLogConfig $config */ $config = app()->make(\SimpleAsFuck\LaravelPerformanceLog\Service\PerformanceLogConfig::class); $config->setSlowJobThreshold(10000); // 10 seconds // no need for threshold restoring, performance listener will handle it
与监控一起使用
建议将性能警告日志发送到您的监控系统中,以便您知道什么运行缓慢。
对于简单的监控,可以使用laravel sentry集成。Sentry集成可以收集有关请求或命令的信息,包括堆栈跟踪,这可以使得查找慢速查询变得更加容易。