Log Route calls in Laravel

Reading time ~1 minute

Introduction

Sometimes when developing a new site or application in Laravel you need to log exactly the route calls in order to track bugs and errors. This operation is straightforward.

First, let’s create a new Middleware

php artisan make:middleware LogRouteMiddleware

Then edit the handle function like the following:

public function handle($request, Closure $next)
{
    Log::info($request->fullUrl());

    return $next($request);
}

The fullUrl method logs the full URL including all url and query parameters. You can decide also to log other information, for example:

  • ip() returns the client IP address
  • isJson() determine if the request is sending JSON

Pro-tip: The API documentation for the Request class is available at http://laravel.com/api/5.1/Illuminate/Http/Request.html

Finally in app/Http/Kernel.php you can include your Middleware to the list

protected $middleware = [
    ...
    'App\Http\Middleware\LogRouteMiddleware',
    ...
];