hesamrad/laravel-flashlight
一个用于记录请求的Laravel包
Requires
- php: ^7.4|^8.0|^8.1|^8.2
- illuminate/config: ^7.0|^8.0|^9.0|^10.0
- illuminate/database: ^7.0|^8.0|^9.0|^10.0
- illuminate/support: ^7.0|^8.0|^9.0|^10.0
Requires (Dev)
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^9.3
README
Laravel Flashlight
Laravel-Flashlight 是一个轻量级的包,用于记录应用中的请求;它高度可定制且易于安装。
如何安装?
按照以下步骤安装 Flashlight。
步骤 #1
使用Composer安装包。
composer require hesamrad/laravel-flashlight
步骤 #2
发布配置文件。
php artisan vendor:publish --provider="HesamRad\Flashlight\FlashlightServiceProvider" --tag="flashlight-config"
步骤 #3
发布迁移文件。(如果您想在数据库中存储日志的话。)
php artisan vendor:publish --provider="HesamRad\Flashlight\FlashlightServiceProvider" --tag="flashlight-migration"
步骤 #4
在路由上应用中间件。
Route::middleware('flashlight')->group(function () { Route::get('/', [SomeController::class, 'index']); });
完成!
现在,所有通过 Flashlight 传输的请求都将被记录以供稍后监控。
注意,所有 Flashlight 日志都存储在 storage/logs/flashlight.log
文件中。
如何自定义 Flashlight?
紧急开关
如果您想暂时开启或关闭 Flashlight,可以通过编辑 App/config/flashlight.php
配置文件中的 enabled
键来实现。
'enabled' => true
注意,默认情况下 Flashlight 是开启的。
驱动选项
您可以选择多个驱动来记录应用中的请求。您可以选择 file
或 database
,分别将日志记录保存在本地文件或数据库表中。您可以通过编辑 App/config/flashlight.php
配置文件中的 driver
键来更改驱动。(此值对应于 drivers
数组中的一个现有键。)
'driver' => 'file'
注意,Flashlight 默认使用 file
驱动。
HTTP方法自定义
如果您想自定义要记录的HTTP方法,可以通过编辑 App/config/flashlight.php
配置文件中的 excluded_methods
数组来实现。
例如
'excluded_methods' => [ 'put', ]
通过这样做,所有 PUT
请求都将被 Flashlight 忽略。
注意,默认情况下 Flashlight 记录所有 HTTP 方法。
记录请求头
如果您想记录请求头,可以通过编辑 App/config/flashlight.php
配置文件中的 log_headers
键来实现。
'log_headers' => true
注意,默认情况下 Flashlight 记录所有请求头。
记录请求体
如果您想记录请求体,可以通过编辑 App/config/flashlight.php
配置文件中的 log_body
键来实现。
'log_body' => true
注意,默认情况下 Flashlight 记录所有请求体。
请求参数自定义
如果您想自定义要记录的请求参数,可以通过编辑 App/config/flashlight.php
配置文件中的 excluded_parameters
数组来实现。
例如
'excluded_parameters' => [ 'password', 'password_confirmation', ]
通过这样做,所有指定的参数都将被 Flashlight 忽略。
注意,默认情况下 Flashlight 忽略 password
和 password_confirmation
参数。
URI自定义
如果您想自定义要记录的URI,可以通过编辑 App/config/flashlight.php
配置文件中的 excluded_uris
数组来实现。
例如
'excluded_uris' => [ 'admins*', ]
通过这样做,所有位于 /admins
下的请求都将被 Flashlight 忽略。
注意,默认情况下 Flashlight 记录所有请求。
日志表太大?
没有问题!手电筒内置了一个修剪命令,用于删除旧日志,以保持存储内部表更轻量。
您只需运行 php artisan flashlight:prune
命令即可实现此功能。
请注意,默认值为30天;手电筒将删除所有超过这个时间的东西;因此,如果您想更改这个值,可以在 App/config
下的 flashlight.php
配置文件中编辑 prune_period
数组。