always-open/laravel-request-logger

Micro-package to ease the effort to log HTTP requests made from your application
103,223
Install
composer require always-open/laravel-request-logger
Latest Version:v3.0.0
PHP:^8.2.0|^8.3.0|^8.4.0
License:MIT
Last Updated:Sep 3, 2025
Links: GitHub  ·  Packagist
Maintainer: qschmick

Laravel Request Logger

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.

Related Packages

always-open/laravel-migration-snapshot

Dump and load snapshots of the schema produced by individual migrations.

45,103 0
always-open/laravel-model-auditlog

Tracks changes made to models and logs them to individual tables.

114,520 6
always-open/laravel-process-stamps

Logs which process created or modified a record

116,619 0
php-tmdb/laravel

Laravel Package for TMDB ( The Movie Database ) API. Provides easy access to the...

53,312 160