mohammad nabipour
mohammad nabipour

Reputation: 77

how to add log when call http request in laravel?

I need to add log after call http request in big project like this?

$response = Http::get('http://example.com');
Log::info(`add request and header and response`);

i want to define global log for all http requests.

i need to define macro like this :

 \Illuminate\Support\Facades\Http::macro('log',function(){
    Log::info(`add request and header and response`);
  });

and call http request like this:

 $response = Http::get('http://example.com')->log();

Upvotes: 1

Views: 6662

Answers (2)

aynber
aynber

Reputation: 23001

Http is built on Guzzle, which accepts cURL options. One of those is CURLOPT_VERBOSE, rewritten as debug, which will send request data to either the screen or a log file. It accepts a file resource as an option:

$response = Http::withOptions(['debug'=>true])->get('http://example.com');

Or

$fp = fopen(storage_path('http_log.txt'), 'w+');
$response = Http::withOptions(['debug'=>$fp])->get('http://example.com');

If you need more data than that, you can extend the Http class and add your own logging methods to it.

See https://laravel.com/docs/8.x/http-client#guzzle-options and https://docs.guzzlephp.org/en/stable/request-options.html#debug for information on the debug option.

Upvotes: 2

MD. Jubair Mizan
MD. Jubair Mizan

Reputation: 1570

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,
            ]);
        }
    }
}

Upvotes: 0

Related Questions