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 stream with PHP Guzzle?
- How to add query parameters to a request with PHP Guzzle?
- How to use Promises with PHP Guzzle (with an example)?
- How to keep alive with Guzzle in PHP?
- How to add a bearer token in PHP Guzzle?
- How to send multiple requests with Guzzle in PHP?
- How to install PHP Guzzle without Composer?
- What version of PHP is required for Guzzle?
- How to update PHP Guzzle?
See more codes...