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
$logger = new \Monolog\Logger('guzzle');- creates a logger object.$logger->pushHandler(new \Monolog\Handler\StreamHandler('guzzle.log'));- adds a handler to the logger object which will write the log messages to theguzzle.logfile.$client = new \GuzzleHttp\Client([- creates a Guzzle client.'handler' => \GuzzleHttp\HandlerStack::create(),- creates a handler stack.'logger' => $logger- adds the logger object to the Guzzle client.
Helpful links
More of Php Guzzle
- How to handle a RequestException with PHP Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
- How to set a timeout for a Guzzle client in PHP?
- How to make an HTTP POST request with JSON using PHP Guzzle?
- How to use PHP Guzzle to make a batch request?
- How to get the response body in JSON format in PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
See more codes...