9951 explained code solutions for 126 technologies


php-guzzleHow to log requests with Guzzle in PHP?


Logging requests with Guzzle in PHP is easy and straightforward. To enable logging, you need to create a logger object and pass it to the Guzzle client.

$logger = new \Monolog\Logger('guzzle');
$logger->pushHandler(new \Monolog\Handler\StreamHandler('guzzle.log'));

$client = new \GuzzleHttp\Client([
    'handler' => \GuzzleHttp\HandlerStack::create(),
    'logger' => $logger
]);

The above code will create a logger object and pass it to the Guzzle client. The logger will write the log messages to the guzzle.log file.

Code explanation

  1. $logger = new \Monolog\Logger('guzzle'); - creates a logger object.
  2. $logger->pushHandler(new \Monolog\Handler\StreamHandler('guzzle.log')); - adds a handler to the logger object which will write the log messages to the guzzle.log file.
  3. $client = new \GuzzleHttp\Client([ - creates a Guzzle client.
  4. 'handler' => \GuzzleHttp\HandlerStack::create(), - creates a handler stack.
  5. 'logger' => $logger - adds the logger object to the Guzzle client.

Helpful links

Edit this code on GitHub