patrikap/laravel-api-logger

A Laravel 5 package to log API requests

dev-master 2019-09-05 19:22 UTC

This package is auto-updated.

Last update: 2024-10-06 07:00:48 UTC


README

Log API requests in Laravel applications

Installation

You can install the package via composer:

composer require patrikap/laravel-api-logger

Optionally you can publish the configfile with:

php artisan vendor:publish --provider="Patrikap\ApiLogger\ApiLoggerServiceProvider" --tag="config" 

Usage

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 = [
    // ...
    
    \Patrikap\ApiLogger\Middlewares\ApiLogger::class
];
// in a routes file

Route::post('/submit-form', function () {
    //
})->middleware(\Patrikap\ApiLogger\Middlewares\ApiLogger::class);

Logging

Two classes are used to handle the logging of incoming requests: a LogProfile class will determine whether the request should be logged, and LogWriter class will write the request to a log.

A default log implementation is added within this package. It will only log GET, POST, PUT, PATCH, and DELETE requests and it will write to the default Laravel logger.

You're free to implement your own log profile and/or log writer classes, and configure it in config/api-logger.php.

A custom log profile must implement \Patrikap\ApiLogger\LogProfile. This interface requires you to implement shouldLogRequest.

// Example implementation from `\Patrikap\ApiLogger\DefaultLogProfile`

public function shouldLogRequest(Request $request): bool
{
   return in_array(strtolower($request->method()), config('api-logger.logged_methods'));
}

A custom log writer must implement \Patrikap\ApiLogger\LogWriter. This interface requires you to implement logRequest and logResponse.

Changelog

Please see CHANGELOG for more information what has changed recently.

Author

Patrikap

Inspiration

This project was inspired by the following projects: