How to log response time for all Guzzle requests

I’m using Guzzle as a http client in a Symfony app. With the following code I’m able to log all requests and responses. Is there a way to log also response time?

I tried on_stats but it should be added to each client call and it is not an option.

services:
  app.client.default:
    class: 'GuzzleHttpClient'
    lazy: true
    arguments:
      - handler: '@app.handler_stack.default'

  app.handler_stack.default:
    class: 'GuzzleHttpHandlerStack'
    factory: [ GuzzleHttpHandlerStack, create ]
    calls:
      - [ push, [ '@app.middleware.log_default', 'log_default' ] ]

  app.middleware.log_default:
    class: callback
    factory: [ GuzzleHttpMiddleware, log ]
    arguments:
      - '@logger'
      - '@app.log_formatter.default'

  app.log_formatter.default:
    class: 'GuzzleHttpMessageFormatter'
    arguments:
      - ">>>>>>>>n{request}n<<<<<<<<n{response}n--------n{error}"

  app.service:
    class: 'AppService'
    arguments:
      - '@app.client.default'
use GuzzleHttpClient;

class Service {

    private $httpClient;

    public function __construct(Client $httpClient) {
       $this->httpClient = $httpClient;
    }

    public function apiRequest() {
        $response = $this->httpClient->get('https://www.google.com');
        // ...
    }
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *