Easily capture every incoming request and the corresponding outgoing response in your Laravel app.

Overview

Easily capture every incoming request and the corresponding outgoing response in your Laravel app.

This package is designed to work only with the Laravel framework.

Installation

Install via composer:

composer require mtownsend/laravel-request-response-logger

Registering the service provider (Laravel users)

For Laravel 5.4 and lower, add the following line to your config/app.php:

/*
 * Package Service Providers...
 */
Mtownsend\RequestResponseLogger\Providers\RequestResponseLoggerServiceProvider::class,

For Laravel 5.5 and greater, the package will auto register the provider for you.

Publish the migration and config files

You will need to publish the migration and configuration file before you can begin using the package. To do so run the following in your console:

php artisan vendor:publish --provider="Mtownsend\RequestResponseLogger\Providers\RequestResponseLoggerServiceProvider"

Next, you need to run the migration for the new database table. Run the following in your console:

php artisan migrate

Setting up the middleware (important!)

In order to begin logging requests and responses you will have to attach the middleware to the routes you want to log. The package does not make this assumption for you, since not everyone may want to log every route.

Now, navigate to your /app/Http/Kernel.php

You can choose which method you would like to use. We've provided a few different options below:

OPTION 1: Bind the middleware to a name you can call only on the routes you want

protected $routeMiddleware = [
    ...
    'log.requests.responses' => \Mtownsend\RequestResponseLogger\Middleware\LogRequestsAndResponses::class,
];

Then apply the named middleware to whatever routes you want:

Route::post('/some/route', SomeController::class)->middleware(['log.requests.responses']);

OPTION 2: Assign the middleware to a route group

protected $middlewareGroups = [
    ...
    'api' => [
        ...
        \Mtownsend\RequestResponseLogger\Middleware\LogRequestsAndResponses::class,
    ],
];

OPTION 3: Assign the middleware to every route

protected $middleware = [
    ...
    \Mtownsend\RequestResponseLogger\Middleware\LogRequestsAndResponses::class,
];

That's it! The middleware will log every incoming request it receives!

Customizing your configuration (optional)

This package provides a few customizations you can make.

When you navigation to app/config you will see a log-requests-and-responses.php file. It will contain the following:

return [

    /**
     * The model used to manage the database table for storing request and response logs.
     */
    'logging_model' => \Mtownsend\RequestResponseLogger\Models\RequestResponseLog::class,

    /**
     * When logging requests and responses, should the logging action be
     * passed off to the queue (true) or run synchronously (false)?
     */
    'logging_should_queue' => false,

    /**
     * If stored json should be transformed into an array when retrieved from the database.
     * Set to `false` to receive as a regular php object.
     */
    'get_json_values_as_array' => true,

];

Housekeeping

You may want to utilize some housekeeping to prevent your logs table from getting too large. This package supplies a preregistered command for wiping the table clean. You may run it manually

php artisan request-response-logger:clean

You may also schedule it to be run automatically in app/Console/Kernel.php:

protected function schedule(Schedule $schedule)
{
    ...
    $schedule->command('request-response-logger:clean')->quarterly();
}

Advanced Usage

Model scopes

If you would like to work with the data you've logged, you may want to retrieve data based on the http code your app returned for that request.

use Mtownsend\RequestResponseLogger\Models\RequestResponseLog;

// Get every logged item with an http response code of 2XX:
RequestResponseLog::successful()->get();

// Get every logged item with an http response code that ISN'T 2XX:
RequestResponseLog::failed()->get();

Replacing the RequestResponseLog model with your own

You may want to extend the base RequestResponseLog model with your own. This is entirely possible.

First, create your own model

php artisan make:model RequestResponseLog

Then in your model, extend the base model:



namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Mtownsend\RequestResponseLogger\Models\RequestResponseLog as BaseRequestResponseLog;

class RequestResponseLog extends BaseRequestResponseLog
{
    use HasFactory;
}

Then in your app/config/log-requests-and-responses.php:

'logging_model' => \App\Models\RequestResponseLog::class,

Now the package will utilize your model instead of the default one.

Testing

You can run the tests with:

vendor/bin/phpunit

License

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

You might also like...
A Laravel response helper methods.
A Laravel response helper methods.

A Laravel response helper methods. The package respond provides a fluent syntax to form array or json responses.

Provides a powerful error response system for Laravel
Provides a powerful error response system for Laravel

Laravel Exceptions Laravel Exceptions was created by, and is maintained by Graham Campbell, and provides a powerful error response system for both dev

Laravel Custom Response Messages from Passport Oauth2 Server

This is a sample repository showing how to install Oauth2 server using Laravel's passport package and customize its responses.

Laravel Response Formatter
Laravel Response Formatter

I created this package to make it easier to format the response from a controller. I have used this package in my projects and I hope you enjoy it!

A Magento Incident Response Plan Template

A Magento centric Incident Response Plan Template Introduction This will provide you with our defined process and procedures to use when responding to

Simple PSR-7 compatible response sender

Simple PSR-7 compatible response sender

🖖Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any kind of filters.
🖖Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any kind of filters.

Repository Repository Pattern in Laravel. The package allows to filter by request out-of-the-box, as well as to integrate customized criteria and any

laravel zibal - transaction request package for zibal
laravel zibal - transaction request package for zibal

laravel zibal transaction request package for zibal Getting Started To get a local copy up and running follow these simple steps. Installation You can

This package can use form request just as Laravel do.

Lumen Form Request This package can use form request just as Laravel do. Installation Install by composer $ composer require chhw/form-request In

Releases(2.0.0)
  • 2.0.0(Oct 8, 2022)

    You can now add your own custom logic to specify if a request/response should be logged. Comes with 3 new adapters out of the box:

    • LogAll - log request & response
    • LogClientErrorsOnly - log only responses that have an http status code of 4XX
    • LogSuccessOnly - log only responses that have an http status code of 2XX

    Upgrade from 1.0.X -> 2.0.X

    1. Install v2.0.X with composer
    2. Add the following to the bottom of your config/log-requests-and-responses.php:
    /**
     * The class responsible for determining if a request should be logged.
     * 
     * Out of the box options are:
     * Mtownsend\RequestResponseLogger\Support\Logging\LogAll::class,
     * Mtownsend\RequestResponseLogger\Support\Logging\LogClientErrorsOnly::class,
     * Mtownsend\RequestResponseLogger\Support\Logging\LogSuccessOnly::class,
     */
    'should_log_handler' => \Mtownsend\RequestResponseLogger\Support\Logging\LogAll::class,
    
    Source code(tar.gz)
    Source code(zip)
  • 1.1.0(Oct 5, 2022)

  • 1.0.0(Aug 3, 2021)

Owner
Mark Townsend
Husband, father, lover of Mexican food, and web developer.
Mark Townsend
This Package helps you in laravel application to log all desired activity for each request from request entry point to generate response at a single snapshot.

Laravel Scenario Logger This Package helps you in laravel application to log all desired activity for each request from request entry point to generat

Mehrdad Mahdian 6 Sep 27, 2021
Catch incoming emails in your Laravel application

Laravel Mailbox ?? Handle incoming emails in your Laravel application. Mailbox::from('{username}@gmail.com', function (InboundEmail $email, $username)

Beyond Code 918 Jan 2, 2023
A package to keep track of outgoing emails in your Laravel application.

Keep track of outgoing emails and associate sent emails with Eloquent models This package helps you to keep track of outgoing emails in your Laravel a

Stefan Zweifel 108 Nov 1, 2022
CSS Exfil helper script to generate injected CSS and corresponding HTML (inspired by mike gualtieri)

The PoC-CSS Exfill Basic Keylogger First of all i was developing bot stuff and i seen attribute=value] [target=_blank] in source code of website. This

Ahsen 6 Apr 2, 2022
With dadjokes every time you load your control panel you'll be greeted by an epic dad joke on the dashboard.

Filament Dad Jokes Widget With DadJokes every time you load your control panel you'll be greeted by an epic dad joke on the dashboard. Installation Yo

Craig Smith 15 Jan 7, 2023
Easily validate data attributes through a remote request

Laravel Remote Rule Easily validate data attributes through a remote request. This package allows you to define a subset of custom rules to validate a

H-FARM Innovation 27 Nov 20, 2022
Simple package to handle response properly in your API.

Simple package to handle response properly in your API. This package uses Fractal and is based on Build APIs You Won't Hate book.

Optania 375 Oct 28, 2022
Simple and ready to use API response wrapper for Laravel.

Laravel API Response Simple Laravel API response wrapper. Installation Install the package through composer: $ composer require obiefy/api-response Re

Obay Hamed 155 Dec 14, 2022
Laravel Logable is a simple way to log http request in your Laravel application.

Laravel Logable is a simple way to log http request in your Laravel application. Requirements php >= 7.4 Laravel version >= 6.0 Installation composer

Sagar 6 Aug 25, 2022
Convert remote api response data into laravel model

laravel remote model Create remote driver to convert remote api request into laravel model. 中文文档 日本語文書 overview Install the version between laravel5.5

张子彬 15 Aug 11, 2022