/laravel-request-logs

Log and debug HTTP requests within your Laravel application.

Primary LanguagePHPMIT LicenseMIT

Laravel Request Logs

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Provide insight to your application's requests with this Laravel-specific package. Simply add a light-weight middleware to your application and capture all the requests' body, headers, and responses. A console command is also provided that can be used to purge the logs to prevent any dated, unwanted bloat.

Installation

You can install the package via composer:

composer require dniccum/laravel-request-logs

You can publish and run the migrations with:

php artisan vendor:publish --tag="laravel-request-logs-migrations"
php artisan migrate --force

You can publish the config file with:

php artisan vendor:publish --tag="laravel-request-logs-config"

This is the contents of the published config file:

return [

    /*
    |--------------------------------------------------------------------------
    | Table Name
    |--------------------------------------------------------------------------
    |
    | The name of the table that will store the log entries.
    |
    */

    'table_name' => 'request_logs',

    /*
    |--------------------------------------------------------------------------
    | History
    |--------------------------------------------------------------------------
    |
    | The number of days of logs that you would like to keep in the database
    | at any time.
    |
    */

    'history' => env('LOGGING_HISTORY', 21),

    /*
    |--------------------------------------------------------------------------
    | Remove Bearer Token
    |--------------------------------------------------------------------------
    |
    | Enable if you would like to remove the bearer authentication token from
    | the request header.
    |
    */

    'remove_bearer' => env('LOGGING_REMOVE_BEARER', true),

];

Usage

Middleware

Adding to Middleware Groups

If you would like to use this package's middleware across a large group of requests, like all of your api routes, you can apply it to your applications api middleware group within your app/Http/Kernel.php like so:

protected $middlewareGroups = [
        'web' => [
            ...
        ],

        'api' => [
            ...
            \Dniccum\LaravelRequestLogs\Http\Middleware\RequestLogging::class,
        ],
    ];

Adding to specific routes

On the other hand, if you are wanting to add some logging clarity to specific routes, you can define it as a route middleware within your app/Http/Kernel.php:

/**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        ...
        'route-logging' => \Dniccum\LaravelRequestLogs\Http\Middleware\RequestLogging::class
    ];

and then assign it to a specific route within your routes file:

Route::post('/create-project', [ \App\Http\Controllers\SampleController::class, 'create' ])
    ->middleware([ 'route-logging' ]);

Log Cleanup (optional)

Note – this is not required but HIGHLY recommended as this could lead to large amounts of data within your database in short periods of time.

To automate the removal of older logs you can either use the provided artisan command php artisan request-logs:clean manually with your app's environment, or you can leverage your app's scheduler process within your app/Console/Kernel.php file to automate it like so:

$schedule->job(\Dniccum\LaravelRequestLogs\Commands\CleanReqeustLogsCommand::class)
    ->daily();

Stored History

By default, this package will retain 21 days of logs. This can be changed via environment variable (LOGGING_HISTORY) or simply modifying it within the request-logs.php configuration file.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

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