|
| 1 | +[](https://packagist.org/packages/webclient/ext-log) |
| 2 | +[](https://packagist.org/packages/webclient/ext-log/stats) |
| 3 | +[](https://github.com/ddrv/php-http-client-wrapper-log/blob/master/LICENSE) |
| 4 | +[](https://php.net) |
| 5 | + |
| 6 | +# webclient/ext-log |
| 7 | + |
| 8 | +Logging decorator for PSR-18 HTTP clients. |
| 9 | + |
| 10 | +# Install |
| 11 | + |
| 12 | +Install this package, your favorite [psr-3 implementation](https://packagist.org/providers/psr/log-implementation) and your favorite [psr-18 implementation](https://packagist.org/providers/psr/http-client-implementation). |
| 13 | + |
| 14 | +```bash |
| 15 | +composer require webclient/ext-log:^1.0 |
| 16 | +``` |
| 17 | + |
| 18 | +# Using |
| 19 | + |
| 20 | +```php |
| 21 | +<?php |
| 22 | + |
| 23 | +use Psr\Http\Client\ClientInterface; |
| 24 | +use Psr\Http\Message\RequestInterface; |
| 25 | +use Psr\Log\LoggerInterface; |
| 26 | +use Webclient\Extension\Log\Client; |
| 27 | + |
| 28 | +/** |
| 29 | + * @var ClientInterface $client |
| 30 | + * @var LoggerInterface $logger |
| 31 | + */ |
| 32 | +$http = new Client($client, $logger); |
| 33 | + |
| 34 | +/** @var RequestInterface $request */ |
| 35 | +$response = $http->sendRequest($request); |
| 36 | +``` |
| 37 | + |
| 38 | +# Custom Request ID |
| 39 | + |
| 40 | +You may implement `\Webclient\Extension\Log\IdGenerator\IdGenerator` for your Request ID in logs |
| 41 | + |
| 42 | +```php |
| 43 | +<?php |
| 44 | + |
| 45 | +use Psr\Http\Client\ClientInterface; |
| 46 | +use Psr\Http\Message\RequestInterface; |
| 47 | +use Psr\Log\LoggerInterface; |
| 48 | +use Webclient\Extension\Log\Client; |
| 49 | +use Webclient\Extension\Log\IdGenerator\IdGenerator; |
| 50 | + |
| 51 | +/** |
| 52 | + * @var ClientInterface $client |
| 53 | + * @var LoggerInterface $logger |
| 54 | + * @var IdGenerator $idGenerator |
| 55 | + */ |
| 56 | +$http = new Client($client, $logger, $idGenerator); |
| 57 | + |
| 58 | +/** @var RequestInterface $request */ |
| 59 | +$response = $http->sendRequest($request); |
| 60 | +``` |
| 61 | + |
| 62 | +# Custom log output |
| 63 | + |
| 64 | +You may implement `\Webclient\Extension\Log\Formatter\Formatter` for your Request/Response output in logs |
| 65 | + |
| 66 | +```php |
| 67 | +<?php |
| 68 | + |
| 69 | +use Psr\Http\Client\ClientInterface; |
| 70 | +use Psr\Http\Message\RequestInterface; |
| 71 | +use Psr\Log\LoggerInterface; |
| 72 | +use Webclient\Extension\Log\Client; |
| 73 | +use Webclient\Extension\Log\Formatter\Formatter; |
| 74 | + |
| 75 | +/** |
| 76 | + * @var ClientInterface $client |
| 77 | + * @var LoggerInterface $logger |
| 78 | + * @var Formatter $formatter |
| 79 | + */ |
| 80 | +$http = new Client($client, $logger, null, $formatter); |
| 81 | + |
| 82 | +/** @var RequestInterface $request */ |
| 83 | +$response = $http->sendRequest($request); |
| 84 | +``` |
| 85 | + |
| 86 | +# Log levels |
| 87 | + |
| 88 | +You may set your log levels |
| 89 | + |
| 90 | +```php |
| 91 | +<?php |
| 92 | + |
| 93 | +use Psr\Http\Client\ClientInterface; |
| 94 | +use Psr\Http\Message\RequestInterface; |
| 95 | +use Psr\Log\LoggerInterface; |
| 96 | +use Psr\Log\LogLevel; |
| 97 | +use Webclient\Extension\Log\Client; |
| 98 | + |
| 99 | +/** |
| 100 | + * @var ClientInterface $client |
| 101 | + * @var LoggerInterface $logger |
| 102 | + */ |
| 103 | +$http = new Client( |
| 104 | + $client, |
| 105 | + $logger, |
| 106 | + null, |
| 107 | + null, |
| 108 | + LogLevel::INFO, // Request log level |
| 109 | + LogLevel::INFO, // Info responses (status codes 1xx) |
| 110 | + LogLevel::INFO, // Success responses (status codes 2xx) |
| 111 | + LogLevel::INFO, // Redirect responses (status codes 3xx) |
| 112 | + LogLevel::EMERGENCY, // Client error responses (status codes 4xx) |
| 113 | + LogLevel::ERROR, // Server error responses (status codes 5xx) |
| 114 | + LogLevel::WARNING // Base HTTP client exceptions |
| 115 | +); |
| 116 | + |
| 117 | +/** @var RequestInterface $request */ |
| 118 | +$response = $http->sendRequest($request); |
| 119 | +``` |
0 commit comments