【问题标题】:how to add log when call http request in laravel?在laravel中调用http请求时如何添加日志?
【发布时间】:2021-09-22 20:04:51
【问题描述】:

我需要在这样的大项目中调用http请求后添加日志吗?

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

我想为所有 http 请求定义全局日志。

我需要像这样定义宏:

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

并像这样调用http请求:

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

【问题讨论】:

    标签: php laravel lumen


    【解决方案1】:

    您可以使用Terminable Middleware 记录已经发送到浏览器的 HTTP 响应。

    要获得总时间,您可以将 microtime(true) 的结果与 laravel 常量 LARAVEL_START 进行比较。该常量定义在框架的入口点bootstrap/autoload.php

    例如,这是一个中间件,它将同时记录 HTTP 标头和系统记录响应时间。由于您可以访问 $request 变量中的当前请求,因此您可以利用它来记录您想要的任何参数

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

    【讨论】:

    • 这是针对请求而不是 http 客户端请求。 https://laravel.com/docs/8.x/http-client
    【解决方案2】:

    Http 建立在 Guzzle 之上,它接受 cURL 选项。其中之一是 CURLOPT_VERBOSE,重写为debug,它将请求数据发送到屏幕或日志文件。它接受文件资源作为选项:

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

    或者

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

    如果您需要更多的数据,您可以扩展 Http 类并向其添加您自己的日志记录方法。

    有关调试选项的信息,请参阅 https://laravel.com/docs/8.x/http-client#guzzle-optionshttps://docs.guzzlephp.org/en/stable/request-options.html#debug

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 2014-06-13
      相关资源
      最近更新 更多