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.log
file.$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 use PHP Guzzle to make a batch request?
- How to set a user agent in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to use cookies with Guzzle in PHP?
- How to retry requests with PHP Guzzle?
- How to use Promises with PHP Guzzle (with an example)?
- How to get the response body in PHP Guzzle?
- How to stream with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
- How to update PHP Guzzle?
See more codes...