hryha/laravel-request-logger

A Laravel package to log requests and responses

2.0.0 2025-04-03 12:25 UTC

This package is auto-updated.

Last update: 2025-04-03 12:44:19 UTC


README

Latest Version on Packagist Total Downloads PHP Version Laravel Version Software License

Laravel request logger

Overview

This package provides middleware that logs incoming HTTP requests and responses in Laravel applications. You can view your logs through a dedicated panel at https://your.domain/request-logs.

Features

  • HTTP request and response logging
  • Web-based log viewer interface
  • Duplicated requests
  • Configurable data retention period
  • Sensitive data masking
  • Support for custom logging fields

Requirements

  • PHP 8.2 or higher
  • Laravel 11 or higher
  • MySQL 5.7 or higher

Installation

Install the package via Composer::

composer require hryha/laravel-request-logger

Run the database migrations:

php artisan migrate

Optionally, publish the package's configuration file:

php artisan vendor:publish --provider="Hryha\RequestLogger\RequestLoggerServiceProvider" 

View the complete configuration options: here.

The package provides middleware that can be registered either globally or on specific routes.

// In bootstrap/app.php for global middleware
return Application::configure(basePath: dirname(__DIR__))
    ->withMiddleware(function (Middleware $middleware): void {
        $middleware->prepend([
            \Hryha\RequestLogger\Http\Middleware\RequestLogger::class,
        ])
    })
// Or in your routes file for specific routes
Route::get('/user', [UserController::class, 'index'])->middleware(\Hryha\RequestLogger\Http\Middleware\RequestLogger::class);

Data Pruning

To prevent the request_logs table from growing too large, schedule the request-logs:clear command to run daily:

$schedule->command('request-logs:clear')->daily();

The request-logs:clear command removes logs older than the number of days specified in your log_keep_days configuration. To delete all logs, use the --all parameter:

php artisan request-logs:clear --all

Custom Fields

The Request Logger supports additional custom fields for enhanced logging capabilities.

Use the RequestLogger::addCustomField(key, value) method to include additional data in your logs. Additional data can be added from anywhere in the application using this code:

resolve(RequestLogger::class)->addCustomField('user_id', Auth::id());

also, to filter logs by this field, you can add this field to the settings

REQUEST_LOGGER_CUSTOM_FIELDS="user_id,other_field"

Ignoring Responses by Status Code

Configure status codes to ignore by setting REQUEST_LOGGER_IGNORE_RESPONSE_STATUSES in your .env file. The setting accepts both status ranges and specific status codes:

REQUEST_LOGGER_IGNORE_RESPONSE_STATUSES="[[100, 299], 301, 302]"

This configuration will ignore logs for responses with status codes between 100-299, as well as 301 and 302 responses.

Panel authorization

Be sure to protect this panel from unauthorized access. We recommend using Basic Auth middleware or something similar. To do this, add an auth middleware in request-logger.php

'middleware' => [
    \Hryha\SimpleBasicAuth\SimpleBasicAuth::class,
],

Testing

Run the test:

composer test