10

I want to log every request that come to the my site with all the parameters and the time it need to generate the response. The site is build in Laravel 5. I did try different approaches but no luck. My main problem is in how to get the total execution time.

Thanks

3 Answers 3

17

You can use a Terminable Middleware to log the HTTP response after it has already been sent to the browser.

To get the total time you can compare the result of microtime(true) with the laravel constant LARAVEL_START. That constant is defined at bootstrap/autoload.php, the entry point of the framework

For instance, here is a middleware that will log in both HTTP headers and system log the response time. Since you have access to the current request in the $request variable you could leverage that to also log any parameters you want

<?php // File: app/Http/Middleware/MeasureResponseTime.php

namespace App\Http\Middleware;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class MeasureResponseTime
{
    /**
     * Handle an incoming HTTP request.
     *
     * @param  \Symfony\Component\HttpFoundation\Request $request
     * @param  \Closure $next
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function handle($request, \Closure $next)
    {
        $response = $next($request);

        // Add response time as an HTTP header. For better accuracy ensure this middleware
        // is added at the end of the list of global middlewares in the Kernel.php file
        if (defined('LARAVEL_START') and $response instanceof Response) {
            $response->headers->add(['X-RESPONSE-TIME' => microtime(true) - LARAVEL_START]);
        }

        return $response;
    }

    /**
     * Perform any final actions for the request lifecycle.
     *
     * @param  \Symfony\Component\HttpFoundation\Request $request
     * @param  \Symfony\Component\HttpFoundation\Response $response
     * @return void
     */
    public function terminate($request, $response)
    {
        // At this point the response has already been sent to the browser so any
        // modification to the response (such adding HTTP headers) will have no effect
        if (defined('LARAVEL_START') and $request instanceof Request) {
            app('log')->debug('Response time', [
                'method' => $request->getMethod(),
                'uri' => $request->getRequestUri(),
                'seconds' => microtime(true) - LARAVEL_START,
            ]);
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

(That constant is defined at bootstrap/autoload.php). For laravel 5.5 it is located in public/index.php
amazing solution, still works in laravel 9
0
  • Blackfire (https://blackfire.io/).

    Its pretty straight forward to install it will show you all the function calls and the time it takes to generate responses from it. This will include the time the processor takes + DB queries etc.

  • Laravel debug bar (not so handy with APIs, but normal laravel applications work great with this)

    https://github.com/barryvdh/laravel-debugbar

Both of the top solutions are pretty much out of box and requires little or no effort setting up.

There is another option but I see its going to be very complex and you might have to build the full stack yourself (not recommended). Here is what can be done

I wouldn't recommend you take the last option. If you are trying to find out which of your route is slow its best to enable the access log of apache/Nginx and run those URLs + parameters using blackfire profiling to understand why a URL is slow.

Comments

0

A way to do this is to add middleware directly to your app which listens and catches all requests and responses you app handles. Then it dumps the log to a text file or send it to an external service.

Refer to the article on the subject.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.