sarahman/laravel-http-request-api-log

该库将 http 请求 api 日志存储到数据库表中。

1.0.3 2024-03-25 20:37 UTC

This package is auto-updated.

Last update: 2024-08-25 21:32:18 UTC


README

Latest Version on Packagist Build Status Scrutinizer Code Quality StyleCI Total Downloads License PHP Version Require

sarahman/laravel-http-request-api-log 包为 Laravel 应用提供了易于使用的功能来记录 http api 日志。所有 api 日志都将存储在 _api_calls 表中。

您可以使用 Sarahman\HttpRequestApiLog\Models\ApiLog 模型检索所有 api 日志。

ApiLog::all();

这里有一个更高级的示例

...
use GuzzleHttp\Psr7\Response;
use Illuminate\Database\Eloquent\Model;
use Sarahman\HttpRequestApiLog\Traits\WritesHttpLogs;

class ApiClient
{
    use WritesHttpLogs;
    ...
    ...
    public function __construct()
    {
        ...
        ...
        $this->enableLogging = false; // Overwrite the logging functionality being enabled or not.
    }
    ...
    ...
    public function sampleMethod()
    {
        ...
        ...
        $this->log('POST', $url, $options, new Response(200));
    }
}

安装

您可以通过 composer 安装此包。

composer require sarahman/laravel-http-request-api-log

接下来,您需要加载服务提供者。

// app/config/app.php
'providers' => [
    ...
    Sarahman\HttpRequestApiLog\HttpRequestApiLogServiceProvider::class,
];

您可以使用以下命令发布配置文件:

php artisan config:publish sarahman/laravel-http-request-api-log

这是已发布配置文件的内容。

return [
    /*
     * If set to false, no api log will be saved to the database.
     */
    'enabled' => true,

    /*
     * This model will be used to log activity.
     * It should extend Illuminate\Database\Eloquent\Model.
     */
    'api_log_model' => \Sarahman\HttpRequestApiLog\Models\ApiLog::class,

    /*
     * This is the database connection that will be used by the migration and
     * the ApiLog model shipped with this package. In case it's not set
     * Laravel's database.default will be used instead.
     */
    'database_connection' => 'mysql',

    /*
     * This is the name of the table that will be created by the migration and
     * used by the ApiLog model shipped with this package.
     */
    'table_name' => '_api_calls',
];

您也可以使用以下命令发布迁移:

php artisan migrate:publish sarahman/laravel-http-request-api-log

现在,您可以通过运行迁移来创建 _api_calls 表。

php artisan migrate

注意: 您可以通过将 enableLogging 属性设置为 false 来禁用特定 api 客户端的 api 记录。

测试

您可能需要进入项目目录并运行以下命令以运行测试代码。

composer test

贡献

欢迎为这个库做出贡献。请提交您的更改并给我们发送 pull requests

安全问题

如果您发现任何与安全相关的问题,请随时在 问题跟踪器 中创建一个问题或在 aabid048@gmail.com 上给我们写信。

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件