mtownsend/laravel-request-response-logger
Composer 安装命令:
composer require mtownsend/laravel-request-response-logger
包简介
Easily capture every incoming request and the corresponding outgoing response in your Laravel app.
README 文档
README
Easily capture every incoming request and the corresponding outgoing response in your Laravel app.
This package is designed to work only with the Laravel framework.
Installation
Install via composer:
composer require mtownsend/laravel-request-response-logger
Upgrading from v1.0.X -> v2.0.X
Please see the release notes here.
Registering the service provider (Laravel users)
For Laravel 5.4 and lower, add the following line to your config/app.php:
/* * Package Service Providers... */ Mtownsend\RequestResponseLogger\Providers\RequestResponseLoggerServiceProvider::class,
For Laravel 5.5 and greater, the package will auto register the provider for you.
Publish the migration and config files
You will need to publish the migration and configuration file before you can begin using the package. To do so run the following in your console:
php artisan vendor:publish --provider="Mtownsend\RequestResponseLogger\Providers\RequestResponseLoggerServiceProvider"
Next, you need to run the migration for the new database table. Run the following in your console:
php artisan migrate
Setting up the middleware (important!)
In order to begin logging requests and responses you will have to attach the middleware to the routes you want to log. The package does not make this assumption for you, since not everyone may want to log every route.
Now, navigate to your /app/Http/Kernel.php
You can choose which method you would like to use. We've provided a few different options below:
OPTION 1: Bind the middleware to a name you can call only on the routes you want
protected $routeMiddleware = [ ... 'log.requests.responses' => \Mtownsend\RequestResponseLogger\Middleware\LogRequestsAndResponses::class, ];
Then apply the named middleware to whatever routes you want:
Route::post('/some/route', SomeController::class)->middleware(['log.requests.responses']);
OPTION 2: Assign the middleware to a route group
protected $middlewareGroups = [ ... 'api' => [ ... \Mtownsend\RequestResponseLogger\Middleware\LogRequestsAndResponses::class, ], ];
OPTION 3: Assign the middleware to every route
protected $middleware = [ ... \Mtownsend\RequestResponseLogger\Middleware\LogRequestsAndResponses::class, ];
That's it! The middleware will log every incoming request it receives!
Customizing your configuration (optional)
This package provides a few customizations you can make.
When you navigation to app/config you will see a log-requests-and-responses.php file. It will contain the following:
return [ /** * The model used to manage the database table for storing request and response logs. */ 'logging_model' => \Mtownsend\RequestResponseLogger\Models\RequestResponseLog::class, /** * When logging requests and responses, should the logging action be * passed off to the queue (true) or run synchronously (false)? */ 'logging_should_queue' => false, /** * If stored json should be transformed into an array when retrieved from the database. * Set to `false` to receive as a regular php object. */ 'get_json_values_as_array' => true, /** * The class responsible for determining if a request should be logged. * * Out of the box options are: * Mtownsend\RequestResponseLogger\Support\Logging\LogAll::class, * Mtownsend\RequestResponseLogger\Support\Logging\LogClientErrorsOnly::class, * Mtownsend\RequestResponseLogger\Support\Logging\LogSuccessOnly::class, */ 'should_log_handler' => \Mtownsend\RequestResponseLogger\Support\Logging\LogAll::class, ];
Housekeeping
You may want to utilize some housekeeping to prevent your logs table from getting too large. This package supplies a preregistered command for wiping the table clean. You may run it manually
php artisan request-response-logger:clean
You may also schedule it to be run automatically in app/Console/Kernel.php:
protected function schedule(Schedule $schedule) { ... $schedule->command('request-response-logger:clean')->quarterly(); }
Advanced Usage
Conditional logging
This package provides support for specifying custom conditions before a request/response is logged to the database.
It comes with 3 default options out of the box:
- LogAll - log request & response
- LogClientErrorsOnly - log only responses that have an http status code of 4XX
- LogSuccessOnly - log only responses that have an http status code of 2XX
- ...or your own!
Creating your own conditional logic is pretty straightforward and can be done in 2 simple steps:
- First, create a custom class that will perform your conditional checks for logging. For demonstration purposes let's say we're going to create a conditional logic check to only log requests made from external services and not your own web app. You can use the following code as a template.
<?php namespace App\Support\Logging; use Illuminate\Http\Request; use Mtownsend\RequestResponseLogger\Support\Logging\Contracts\ShouldLogContract; class LogExternalRequests implements ShouldLogContract { public $request; public $response; public function __construct(Request $request, $response) { $this->request = $request; $this->response = $response; } /** * Return a truth-y value to log the request and response. * Return false-y value to skip logging. * * @return bool */ public function shouldLog(): bool { // Custom logic goes here... } }
- Open up your
config/log-requests-and-responses.phpand set theshould_log_handlerkey to your class.
[
//...
'should_log_handler' => \App\Support\Logging\LogExternalRequests::class,
]
...and that's it! Your custom logging logic will now be used any time the middleware is executed.
Model scopes
If you would like to work with the data you've logged, you may want to retrieve data based on the http code your app returned for that request.
use Mtownsend\RequestResponseLogger\Models\RequestResponseLog; // Get every logged item with an http response code of 2XX: RequestResponseLog::successful()->get(); // Get every logged item with an http response code that ISN'T 2XX: RequestResponseLog::failed()->get();
Replacing the RequestResponseLog model with your own
You may want to extend the base RequestResponseLog model with your own. This is entirely possible.
First, create your own model
php artisan make:model RequestResponseLog
Then in your model, extend the base model:
<?php namespace App\Models; use Mtownsend\RequestResponseLogger\Models\RequestResponseLog as BaseRequestResponseLog; class RequestResponseLog extends BaseRequestResponseLog { // }
Then in your app/config/log-requests-and-responses.php:
'logging_model' => \App\Models\RequestResponseLog::class,
Now the package will utilize your model instead of the default one.
Testing
You can run the tests with:
vendor/bin/phpunit
License
The MIT License (MIT). Please see License File for more information.
mtownsend/laravel-request-response-logger 适用场景与选型建议
mtownsend/laravel-request-response-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.84k 次下载、GitHub Stars 达 25, 最近一次更新时间为 2021 年 08 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logging」 「debugging」 「response」 「request」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mtownsend/laravel-request-response-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mtownsend/laravel-request-response-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mtownsend/laravel-request-response-logger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A PHP package that takes a snapshot of your application and allows you to retrieve it later to help with debugging.
A Zend Framework module that sets up Monolog for logging in applications.
HTTP request logger middleware for Laravel
Laravel Database Query Logger and debug helper.
Asynchronous Sentry for Symfony - Fire and forget
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
统计信息
- 总下载量: 11.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 27
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-08-03