Creator HTTP Logger

goedemiddag/request-response-log

Laravel middleware for logging HTTP requests and responses, both incoming and outgoing, with support for the HTTP client, Guzzle and Saloon.

Request Response Log

Laravel package providing middleware to log HTTP requests and responses, both incoming to and outgoing from your application. Originally created to log API requests to other services and to log incoming webhooks, but can be used for any request and response logging.

It offers:

  • Middleware to be used with the Laravel HTTP client, Guzzle and Saloon.
  • Middleware to log requests made to your application and its response.
  • Functionality to manually log requests and responses.

Requirements#

This package requires Laravel 11+ and PHP 8.3+.

Installation#

You can install the package via composer:

composer require goedemiddag/request-response-log

The package will automatically register itself in Laravel, but you need to run the migrations:

php artisan migrate

Configuration#

Publish the config file to customize table names, the database connection and field masking per vendor:

php artisan vendor:publish --provider="Goedemiddag\RequestResponseLog\RequestResponseLogServiceProvider" --tag="config"

This will publish config/request-response-log.php.

Usage#

HTTP Logger#

Use the RequestResponseLogger::middleware() to log outgoing HTTP requests.

Laravel HTTP client#

use Goedemiddag\RequestResponseLog\RequestResponseLogger;
use Illuminate\Support\Facades\Http;

Http::withMiddleware(RequestResponseLogger::middleware('vendor'));

Guzzle#

use Goedemiddag\RequestResponseLog\RequestResponseLogger;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\CurlHandler;

$stack = new HandlerStack();
$stack->setHandler(new CurlHandler());
$stack->push(RequestResponseLogger::middleware('vendor'));

$client = new Client(['handler' => $stack]);

Saloon#

use Goedemiddag\RequestResponseLog\RequestResponseLogger;
use Saloon\Http\Connector;

class YourConnector extends Connector
{
    public function __construct()
    {
        $this
            ->sender()
            ->addMiddleware(RequestResponseLogger::middleware('vendor'));
    }
}

Application Logger#

Register the ApplicationRequestResponseLogger middleware to log requests made to your application and its response:

Route::post('/webhook')
    ->uses([WebhookController::class, 'handle'])
    ->middleware([ApplicationRequestResponseLogger::class]);

Manual Logger#

When you need more control, use the ManualRequestResponseLogger:

use Goedemiddag\RequestResponseLog\Enums\RequestFlow;
use Goedemiddag\RequestResponseLog\ManualRequestResponseLogger;

$requestLog = ManualRequestResponseLogger::fromRequest(
    vendor: 'vendor',
    request: $request,
    flow: RequestFlow::Incoming,
);

// your code here

ManualRequestResponseLogger::fromResponse(
    requestLog: $requestLog,
    response: $response,
);

Backtrace Resolvers#

Control how much backtrace information is logged with a resolver:

  • IgnoreResolver — default, no backtrace logged
  • DefaultResolver — full PHP backtrace
  • LaravelApplicationResolver — optimized backtrace for Laravel applications
use Goedemiddag\RequestResponseLog\Support\BacktraceResolvers\DefaultResolver;

RequestResponseLogger::middleware('vendor', new DefaultResolver());

LaravelApplicationResolver allows you to filter out noise from the backtrace:

new LaravelApplicationResolver(
    includeIndex: false,
    includeVendor: false,
    includeMiddleware: false,
    includePipeline: false,
    includeRouting: false,
);

Additional Context#

Pass extra context data alongside the log entry:

RequestResponseLogger::middleware(
    vendor: 'vendor',
    context: static fn (): array => ['key' => 'value'],
);

The context parameter accepts an array or a closure returning an array.

Request Identifier#

Use Laravel Context to correlate multiple log entries for the same request. Set this before the middleware is called:

Context::add('request-identifier', 'your-identifier');

Cleaning Up#

Schedule model pruning to keep your log table from growing indefinitely. Add this to routes/console.php (Laravel 11+):

use Goedemiddag\RequestResponseLog\Models\RequestLog;
use Illuminate\Support\Facades\Schedule;

Schedule::command('model:prune', ['--model' => [RequestLog::class]])->daily();

Tests#

Unit tests are available in the tests folder. Run with:

composer test

For a code coverage report generated in the build/report folder:

composer test-coverage

Contribution#

Any contribution is welcome, but it should meet the PSR-12 standard and please create one pull request per feature/bug. In exchange, you will be credited as contributor on this page.

Security#

If you discover any security related issues in this or other packages of Goedemiddag, please email info@vdhicts.nl instead of using the issue tracker.

License#

This package is open-sourced software licensed under the MIT license.