-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added RepositoryInterface to implement different log saving options. - Added SyncRepository to save as in version 1.0 - Added QueueRepository to save through a queue to bypass the database transaction problem repository implementations - Added the repository property in Bootstrap to configure the repository. - Static create methods from Request, Response, Exception replaced with Factory class - Add docker-compose.yml to .gitattributes export-ignore
- Loading branch information
Showing
20 changed files
with
574 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Yii2 Guzzle Changelog | ||
|
||
## Release 2.0 | ||
- Added RepositoryInterface to implement different log saving options. | ||
- Added [SyncRepository](./src/Log/SyncRepository.php) to save as in version 1.0 | ||
- Added [QueueRepository](./src/Log/QueueRepository.php) to save through a queue to bypass | ||
the database transaction problem repository implementations | ||
- Added the repository property in [Bootstrap](./src/Bootstrap.php) to configure the repository. | ||
- Static create methods from Request, Response, Exception replaced with [Factory](./src/Log/Factory.php) class |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ Library for storing http queries into database | |
|
||
## Installation | ||
```bash | ||
composer require wearesho-team/yii2-guzzle | ||
composer require wearesho-team/yii2-guzzle:^2.0 | ||
``` | ||
|
||
## Usage | ||
|
@@ -44,6 +44,48 @@ return [ | |
Note: for not UTF-8 request or response body (for example, files) | ||
`(invalid UTF-8 bytes)` will be saved. | ||
|
||
### Log using Queue | ||
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](./src/Log/QueueRepository.php) | ||
to save the logs. To do this, configure Bootstrap this way: | ||
|
||
```php | ||
<?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](./src/Log/RepositoryInterface.php) and use it. | ||
|
||
## TODO | ||
- Tests for [Job](./src/Log/Job.php) | ||
|
||
## Contributors | ||
- [Alexander <horat1us> Letnikow](mailto:[email protected]) | ||
- [Roman <KartaviK> Varkuta](mailto:[email protected]) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Wearesho\Yii\Guzzle\Log; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
class Factory | ||
{ | ||
public const NOT_UTF_8_BODY = "(invalid UTF-8 bytes)"; | ||
|
||
public function fromRequest(RequestInterface $request): array | ||
{ | ||
$body = (string)$request->getBody(); | ||
if (!mb_check_encoding($body, 'UTF-8')) { | ||
$body = static::NOT_UTF_8_BODY; | ||
} | ||
|
||
return [ | ||
'method' => $request->getMethod(), | ||
'uri' => (string)$request->getUri(), | ||
'headers' => $request->getHeaders(), | ||
'body' => $body, | ||
]; | ||
} | ||
|
||
public function fromResponse(ResponseInterface $response): array | ||
{ | ||
$body = (string)$response->getBody(); | ||
if (!mb_check_encoding($body, "UTF-8")) { | ||
$body = static::NOT_UTF_8_BODY; | ||
} | ||
|
||
return [ | ||
'status' => $response->getStatusCode(), | ||
'headers' => $response->getHeaders(), | ||
'body' => $body, | ||
]; | ||
} | ||
|
||
public function fromException(\Throwable $exception): array | ||
{ | ||
$trace = array_map( | ||
fn(array $data): array => array_intersect_key($data, array_flip([ | ||
'file', | ||
'line', | ||
'function', | ||
'class', | ||
])), | ||
$exception->getTrace() | ||
); | ||
return [ | ||
'type' => get_class($exception), | ||
'trace' => $trace, | ||
]; | ||
} | ||
} |
Oops, something went wrong.