liveteched/request-logs

存储接收到的API调用的请求/响应数据和头部信息

v1.0.1 2021-05-10 14:57 UTC

This package is auto-updated.

Last update: 2024-09-24 21:47:42 UTC


README

liveteched/request-logs 包提供了一种存储API调用请求/响应数据以及头部信息和自定义属性的方法。该包将所有请求存储在 request_logs 表中,并允许在 request_log_relations 表中定义多态关系。

该包不提供查看日志的视图,如果您需要,可以自行实现。

request_logs 表中有两个字段是可选的

  • channel - 用于为API设置唯一的通道名称。例如:client1-api
  • action - 用于设置API操作。例如:loginretrieveevent_modified

这两个字段都用于更容易地搜索和处理数据库记录

安装

$ composer require shambou/request-logs
$ php artisan requestlogs:install
$ php artisan migrate

配置

return [
    /*
     * Currently supports only json and soap channels
     * Keep in mind that all channels must be defined in single dimension:
     * ex: 'json' => ['client-api', 'second-client-api']
     *
     * Used to parse request/response data from DB
     * ex:
     * {
     *   "data1": "something",
     *   "data2": "something",
     *   "data3": "something"
     * }
     *
     * will become:
     *
     * data1: something\n
     * data2: something\n
     * data3: something
     *
     * This is attached automatically to \Models\RequestLog parsed_request and parsed_response attributes
     */
    'channels' => [
        'json' => [],
        'soap'  => []
    ]
];

示例用法 1:REST

$startTime = microtime(true);
$response = response()->json([
    'success' => true
]);

$relation = User::find(1);

RequestLogFactory::createForRest($request)
    ->setJsonResponse($response)
    ->setAction('login')
    ->setChannel('client-api')
    ->setExectionTime(microtime(true) - $startTime)
    ->setCustomData([
        'transaction_id' => 'ecfe78cc-10ce-49d2-bb31-29b01da03fc6'
    ])
    ->storeLog($relation);

示例用法 2:SOAP

$wsdlUrl = 'https://api.example.com/soap/V10023.ASMX?WSDL'
$startTime = microtime(true);
$relation = User::find(1);

RequestLogFactory::createForSoap($wdsUrl, $soapClient)
    ->setAction('login')
    ->setChannel('client-soap-api')
    ->setExectionTime(microtime(true) - $startTime)
    ->setCustomData([
        'transaction_id' => 'ecfe78cc-10ce-49d2-bb31-29b01da03fc6'
    ])
    ->storeLog($relation);