Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log actions in Laravel and execution time

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

like image 242
sid606 Avatar asked Sep 29 '15 08:09

sid606


People also ask

What is logs in Laravel?

Laravel logging is based on "channels". Each channel represents a specific way of writing log information. For example, the single channel writes log files to a single log file, while the slack channel sends log messages to Slack. Log messages may be written to multiple channels based on their severity.

How do I view Laravel logs?

You can see the generated log entries in storage/logs/laravel. log file.

Where are Laravel logs stored?

By default, Laravel is configured to create a single log file for your application, and this file is stored in app/storage/logs/laravel. log .


1 Answers

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,
            ]);
        }
    }
}
like image 158
Javi Stolz Avatar answered Oct 05 '22 06:10

Javi Stolz