sarahman/laravel-http-request-api-log

This library stores the http request api log into a database table.

1.0.3 2024-03-25 20:37 UTC

This package is auto-updated.

Last update: 2024-11-25 22:00:17 UTC


README

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

The sarahman/laravel-http-request-api-log package provides easy to use functionality to log the http api logs in the Laravel application. All the api logs will be stored in the _api_calls table.

You can retrieve all api logs using the Sarahman\HttpRequestApiLog\Models\ApiLog model.

ApiLog::all();

Here's a more advanced example:

...
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));
    }
}

Installation

You can install the package via composer:

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

Next, you need to load the service provider:

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

You can publish the config file with:

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

This is the contents of the published config file:

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',
];

You can also publish the migration with:

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

Now, you can create the _api_calls table by running the migrations:

php artisan migrate

N.B.: You can disable the api logging to a specific api client using the enableLogging property setting as false.

Testing

You might go to the project directory and run the following command to run test code.

composer test

Contribution

Feel free to contribute in this library. Please make your changes and send us pull requests.

Security Issues

If you discover any security related issues, please feel free to create an issue in the issue tracker or write us at aabid048@gmail.com.

License

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