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 set a user agent in PHP Guzzle?
- How to set a timeout for a request with PHP Guzzle?
- How to add an SSL certificate to a request with PHP Guzzle?
- How to handle a RequestException with PHP Guzzle?
- How to make an asynchronous send with PHP Guzzle?
- How to convert a response to an array with PHP Guzzle?
- What version of PHP is required for Guzzle?
- How to make HTTP request with PHP Guzzle?
- How to stream with PHP Guzzle?
- How to troubleshoot cURL error 60 with Guzzle in PHP?
See more codes...