dniccum / laravel-request-logs
Log and debug HTTP requests within your Laravel application.
Requires
- php: ^8.0|^8.1|^8.2
- illuminate/contracts: ^9.0|^10.0
- laravel/framework: ^8.0|^9.0|^10.0
- spatie/laravel-package-tools: ^1.13.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
README
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' ]);
IMPORTANT: Response Body Length Limit
Assuming that your database migration and model are unchanged, there is currently logic in place that will prevent the response_body
column from being overloaded. What does this mean? In the event that the response body of the request that you are logging is very large (ie returning 500+ results from an API), the number of bytes/characters might be too large for the database to store without failing. If the response body is longer than 65,000 characters, the response body will NOT be saved.
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.