hryha/request-logger

A Laravel/Lumen package to log requests and responses

v1.0.19 2024-01-23 14:35 UTC

README

Laravel request logger

Log request and response

This package adds a middleware which can log incoming requests and responses. You can see the logs in view panel at https://your.domain/request-logs

Laravel Installation

You can install the package via composer:

composer require hryha/request-logger

You must publish the asset files with:

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

You must publish the config file and assets with:

request-logger.php ```php return [ 'locale' => env('REQUEST_LOGGER_LOCALE', 'en'), // available 'en', 'ua' 'middleware' => [ \Hryha\RequestLogger\Http\Middleware\BaseAuth::class, ], 'base_auth' => [ 'login' => env('REQUEST_LOGGER_LOGIN', 'login'), 'password' => env('REQUEST_LOGGER_PASSWORD', 'password'), ], 'timezone' => env('REQUEST_LOGGER_TIMEZONE', env('APP_TIMEZONE', config('app.timezone', 'UTC'))), 'date_format' => env('REQUEST_LOGGER_DATE_FORMAT', 'Y-m-d'), 'time_format' => env('REQUEST_LOGGER_TIME_FORMAT', 'H:i:s.u'), 'log_keep_days' => env('REQUEST_LOGGER_KEEP_DAYS', 14), 'table_name' => 'request_logs', 'enabled' => env('REQUEST_LOGGER_ENABLED', true), 'ignore_paths' => [ 'request-logs*', 'telescope*', 'nova-api*', ], 'hide_fields' => [ 'request' => [ 'headers' => [ 'authorization', 'php-auth-user', 'php-auth-pw', ], 'content' => [ 'password', 'token', 'access_token', ], ], 'response' => [ 'content' => [ 'password', 'token', 'access_token', ], ], ], 'replacer_hidden_fields' => '|^_-|', ]; ```

You have to execute migrations with:

    php artisan migrate --path=/vendor/hryha/request-logger/database/migrations/2021_11_05_000000_create_request_log_fingerprints_table.php
    php artisan migrate --path=/vendor/hryha/request-logger/database/migrations/2021_11_05_000000_create_request_logs_table.php

This packages provides a middleware which can be added as a global middleware or as a single route.

// in `app/Http/Kernel.php`

protected $middleware = [
    // ...
    
    \Hryha\RequestLogger\Http\Middleware\RequestLogger::class
];
// in a routes file

Route::post('/test', function () {
    //
})->middleware(\Hryha\RequestLogger\Http\Middleware\RequestLogger::class);

Supported drivers

  • storage logs
  • database (mysql)

Lumen Installation

You can install the package via composer:

composer require hryha/request-logger --dev

You must install vendor:publish plugin

You must register provider:

//in 'bootstrap/app.php'

$app->register(\Hryha\RequestLogger\RequestLoggerServiceProvider::class); 

You must publish the config file and assets with:

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

This is the contents of the published config file:

request-logger.php ```php return [ 'locale' => env('REQUEST_LOGGER_LOCALE', 'en'), // available 'en', 'ua' 'middleware' => [ \Hryha\RequestLogger\Http\Middleware\BaseAuth::class, ], 'base_auth' => [ 'login' => env('REQUEST_LOGGER_LOGIN', 'login'), 'password' => env('REQUEST_LOGGER_PASSWORD', 'password'), ], 'timezone' => env('REQUEST_LOGGER_TIMEZONE', env('APP_TIMEZONE', config('app.timezone', 'UTC'))), 'date_format' => env('REQUEST_LOGGER_DATE_FORMAT', 'Y-m-d'), 'time_format' => env('REQUEST_LOGGER_TIME_FORMAT', 'H:i:s.u'), 'log_keep_days' => env('REQUEST_LOGGER_KEEP_DAYS', 14), 'table_name' => 'request_logs', 'enabled' => env('REQUEST_LOGGER_ENABLED', true), 'ignore_paths' => [ 'request-logs*', 'telescope*', 'nova-api*', ], 'hide_fields' => [ 'request' => [ 'headers' => [ 'authorization', 'php-auth-user', 'php-auth-pw', ], 'content' => [ 'password', 'token', 'access_token', ], ], 'response' => [ 'content' => [ 'password', 'token', 'access_token', ], ], ], 'replacer_hidden_fields' => '|^_-|', ]; ```

You must register this config file:

//in 'bootstrap/app.php'

$app->configure('request-logger');

You must execute migrations with:

php artisan migrate

You must create storage symbolic link with:

php artisan storage:link

You must register middleware:

// in 'bootstrap/app.php'

$app->routeMiddleware([
    // ...
    
    'request-logger' => \Hryha\RequestLogger\Http\Middleware\RequestLogger::class,
]);

This packages provides a middleware which can be added as a global middleware or as a single route.

// in a routes file

Route::post('/test', ['uses' => 'TestController@test', 'middleware' => ['request-logger']]);

Data Pruning

Without pruning, the request_logs table can accumulate records very quickly. To mitigate this, you should schedule the request-logs:clear artisan command to run daily:

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

Running the php artisan request-logs:clear command deletes recorded logs older than the number of days specified in the log_keep_days configuration.

To delete all logs, add the "--all" parameter php artisan request-logs:clear --all

Configuration custom fields

The Request Logger allows you to add custom fields for logging. The package provides the configureRequestLoggerCustomFields function, which must return an array containing the custom fields. Additionally, make sure to add these custom fields to the database table request_logs.

Here's an example of using configureRequestLoggerCustomFields():

  • Add the middleware ConfigureRequestLoggerMiddleware.
  • In the middleware, implement the handle method and specify custom fields using the configureRequestLoggerCustomFields function.
final class ConfigureRequestLoggerMiddleware
{
	public function handle(Request $request, Closure $next)
	{
		configureRequestLoggerCustomFields(function () use ($request) {
			return ['user_id' => $request->user()->id ?? null];
		});

		return $next($request);
	}
}
  • Register the middleware in the Http Kernel.

These steps outline how to use the configureRequestLoggerCustomFields function to add custom fields to your logging setup.

Testing

php vendor/bin/phpunit