always-open/laravel-request-logger 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

always-open/laravel-request-logger

最新稳定版本:v3.0.0

Composer 安装命令:

composer require always-open/laravel-request-logger

包简介

Micro-package to ease the effort to log HTTP requests made from your application

README 文档

README

Latest Version on Packagist Build Status GitHub Workflow Status (branch) Packagist Downloads

Maintainability

When making HTTP requests to external APIs it is valuable to track each request and its response. This insight can help you find issues, track usage, and reuse responses for testing/development.

Installation

You can install the package via composer:

composer require always-open/laravel-request-logger

Breaking change

If you are upgrading to 3.x or newer, the following steps must be taken as new fields have been added to the logging tables.

  • Create a migration for each logging tables with the following
// The headers that were part of the request
$table->json('request_headers')
    ->nullable();
// The headers that were part of the response
$table->json('response_headers')
    ->nullable();

Run this migration prior to upgrading the package.

Configuration

php artisan vendor:publish --provider="\AlwaysOpen\RequestLogger\RequestLoggerServiceProvider"

Running the above command will publish the config file.

Usage

Creation

To add logs to your system you must first create the migration and model for the appropriate log. This is done by using the packages request-logger:make-table command.

The command needs the name of the item to be tracked and it will be used for naming the model and table.

Example

php artisan request-logger:make-table facebook

This will create a model \App\Models\FacebookRequestLog and a migration to create the table facebook_request_logs

Implementation

Then you can use that model to create logs of your requests where you can make the API calls.

Example

Guzzle
function makeFacebookApiCall(array $body, Client $facebook_client)
{
    $request_headers = [
        'api-key' => $config->apiKey,
        'Content-Type' => 'application/json',
    ];

    $versioned_path = self::buildVersionedUrlPath($path);

    $encoded_body = json_encode($body, JSON_UNESCAPED_SLASHES);

    $request = new Request(
        'GET',
        '/v1/users',
        $request_headers,
        $encoded_body,
    );
    
    $request_log = FacebookRequestLog::makeFromGuzzle($request);
    
    $response = $client->send($request);
    
    $request_log->response_code = $response->getStatusCode();
    $request_log->response = json_decode((string)$response->getBody(), true);
    $request_log->save();
}

Instead of manually setting the response data you can instead leverage the updateFromResponse method:

function makeFacebookApiCall(array $body, Client $facebook_client)
{
    $request_headers = [
        'api-key' => $config->apiKey,
        'Content-Type' => 'application/json',
    ];

    $versioned_path = self::buildVersionedUrlPath($path);

    $encoded_body = json_encode($body, JSON_UNESCAPED_SLASHES);

    $request = new Request(
        'GET',
        '/v1/users',
        $request_headers,
        $encoded_body,
    );
    
    $request_log = FacebookRequestLog::makeFromGuzzle($request);
    
    $response = $client->send($request);
    $request_log->updateFromResponse($response);
}

You can also manually set each property and then save the log instance.

Testing

composer test

Using Docker

All assets are set up under the docker-compose.yml file. The first time you run the docker image you must build it with the following command:

./docker.sh -b -s

Then you can bring it up in the background using:

./docker.sh -d

From there you can run the tests within an isolated environment

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email @qschmick instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 9.29k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 2
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-04-21