Sometimes it's useful to log some/all requests to your application. This is really convenient when you use Laravel to build your APIs.

As the docs says:

A logging middleware might log all incoming requests to your application.

For me the easiest way to implement something that logs every request made to my application, is to create a middleware. And to be specific this middleware is defined as "Terminable Middleware" because it's going to be called after the HTTP response has already been sent to the browser. This way we have access to both $request and $response at the same time.

There's a slight difference in defining Terminable Middlewares in Laravel 5.0 and Laravel 5.1.

Let's take a look at how we are going to achieve this in Laravel 5.0:

Create a new class within your App\Http\Middleware directory and let's call it LogAfterRequest. Then put this code in it.

use Closure;  
use Illuminate\Contracts\Routing\TerminableMiddleware;  
use Illuminate\Support\Facades\Log;

class LogAfterRequest implements TerminableMiddleware {

    public function handle($request, Closure $next)
        return $next($request);

    public function terminate($request, $response)
        Log::info('app.requests', ['request' => $request->all(), 'response' => $response]);


This middleware implements TerminableMiddleware and in addition to defining a handle method, we have to implement terminate method. And this method recieves $request and $response. These are objects which will give you all of the handy stuffs you probably need to log.

When we have our middleware ready, we should add it to our HTTP Kernel.

Open Kernel.php and add this line to your protected $middleware property:


That's it. You can additionally filter what you want to actually log, but this is the basics.

As I mentioned defining a terminable middleware in Laravel 5.1 is sligtly different, because TerminableMiddleware interface is deprecated.

To define a middleware as Terminable simply add a method terminate and do the same thing as in *Laravel 5.0.