Library for storing http queries into database
composer require wearesho-team/yii2-guzzle:^2.0
- Append Bootstrap to your application
<?php
use Wearesho\Yii\Guzzle;
use Psr\Http\Message\RequestInterface;
return [
'bootstrap' => [
'http-log' => [
'class' => Guzzle\Bootstrap::class,
// Logs exclude rules
'exclude' => [
// url regular expression
'/^.*(google).*$/iu',
// or closure (return true if you don't need to log request)
fn(Message\RequestInterface $request): bool => $request->getUri()->getHost() === 'zsu.gov.ua/'
],
// Guzzle client configuration settings
'config' => [
'timeout' => 10,
],
],
],
];
- Use
\Yii::$container
to instantiate GuzzleHttp\Client and send requests to log them - Use Guzzle\Log\Request, Guzzle\Log\Response, Guzzle\Log\Exception to find logs
Note: for not UTF-8 request or response body (for example, files)
(invalid UTF-8 bytes)
will be saved.
Sometimes it is not possible to use synchronous saving of query logs when the query was executed during an initiated database transaction. In this case it is possible that the transaction will not be saved and the logs will be lost.
The recommended solution is to use QueueRepository to save the logs. To do this, configure Bootstrap this way:
<?php
use Wearesho\Yii\Guzzle;
use Psr\Http\Message\RequestInterface;
return [
'bootstrap' => [
'http-log' => [
'class' => Guzzle\Bootstrap::class,
// Here you configure repository
'repository' => [
'class' => Guzzle\Log\QueueRepository::class,
],
// Logs exclude rules
'exclude' => [
// your rules
],
// Guzzle client configuration settings
'config' => [
'timeout' => 10,
],
],
],
];
Also, you have the option to implement your own RepositoryInterface and use it.
- Tests for Job