PHPackages                             mtownsend/laravel-request-response-logger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Logging &amp; Monitoring](/categories/logging)
4. /
5. mtownsend/laravel-request-response-logger

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

mtownsend/laravel-request-response-logger
=========================================

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

2.0.0(3y ago)2511.3k↓100%2MITPHPPHP &gt;=7.0|^8.0

Since Aug 3Pushed 3y ago1 watchersCompare

[ Source](https://github.com/mtownsend5512/laravel-request-response-logger)[ Packagist](https://packagist.org/packages/mtownsend/laravel-request-response-logger)[ RSS](/packages/mtownsend-laravel-request-response-logger/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (2)Versions (4)Used By (0)

[![](https://camo.githubusercontent.com/161af0059a74f7f765935dd5ad7be34355a6a3df722c681ecf5a52f0b6dd3aa1/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c25323052657175657374253230253236253230526573706f6e73652532304c6f676765722e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6d746f776e73656e642532466c61726176656c2d726571756573742d726573706f6e73652d6c6f67676572267061747465726e3d706c7573267374796c653d7374796c655f31266465736372697074696f6e3d436170747572652b796f75722b696e636f6d696e672b72657175657374732b616e642b636f72726573706f6e64696e672b726573706f6e7365732b776974682b65617365266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d3735707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)](https://camo.githubusercontent.com/161af0059a74f7f765935dd5ad7be34355a6a3df722c681ecf5a52f0b6dd3aa1/68747470733a2f2f62616e6e6572732e6265796f6e64636f2e64652f4c61726176656c25323052657175657374253230253236253230526573706f6e73652532304c6f676765722e706e673f7468656d653d6461726b267061636b6167654d616e616765723d636f6d706f7365722b72657175697265267061636b6167654e616d653d6d746f776e73656e642532466c61726176656c2d726571756573742d726573706f6e73652d6c6f67676572267061747465726e3d706c7573267374796c653d7374796c655f31266465736372697074696f6e3d436170747572652b796f75722b696e636f6d696e672b72657175657374732b616e642b636f72726573706f6e64696e672b726573706f6e7365732b776974682b65617365266d643d312673686f7757617465726d61726b3d3026666f6e7453697a653d3735707826696d616765733d68747470732533412532462532466c61726176656c2e636f6d253246696d672532466c6f676f6d61726b2e6d696e2e737667)

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
------------

[](#installation)

Install via composer:

```
composer require mtownsend/laravel-request-response-logger

```

Upgrading from v1.0.X -&gt; v2.0.X
----------------------------------

[](#upgrading-from-v10x---v20x)

[Please see the release notes here.](https://github.com/mtownsend5512/laravel-request-response-logger/releases/tag/2.0.0)

### Registering the service provider (Laravel users)

[](#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

[](#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!)

[](#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

[](#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

[](#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

[](#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)

[](#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,

    /**
     * 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,

];
```

Housekeeping
------------

[](#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
==============

[](#advanced-usage)

Conditional logging
-------------------

[](#conditional-logging)

This package provides support for specifying custom conditions before a request/response is logged to the database.

It comes with 3 default options out of the box:

- LogAll - log request &amp; 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
- ...or your own!

Creating your own conditional logic is pretty straightforward and can be done in 2 simple steps:

1. First, create a custom class that will perform your conditional checks for logging. For demonstration purposes let's say we're going to create a conditional logic check to only log requests made from external services and not your own web app. You can use the following code as a template.

```
