liborm85 / loggable-http-client
Extended logging for the Symfony HTTP Client.
Installs: 1 916
Dependents: 0
Suggesters: 0
Security: 0
Stars: 7
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=8.1
- psr/log: ^1.0|^2.0|^3.0
- symfony/http-client-contracts: ^3.0
Requires (Dev)
- ext-curl: *
- ext-json: *
- amphp/http-client: ^4.2.1
- phpunit/phpunit: ^10.0
- symfony/http-client: ^6.1
- symfony/process: ^6.1
Suggests
- symfony/http-client: This package requires an actual Symfony HTTP client implementation to decorate.
Provides
- psr/log-implementation: 1.0|2.0|3.0
- symfony/http-client-implementation: 3.0
README
Extended logging for the Symfony HTTP Client allowing especially to log the content of the request and response.
Adds events to PSR-3 logger interface:
'Response content:'
(level:info
) - response body content received; in context is available (details in Usage):request
(\Liborm85\LoggableHttpClient\Context\RequestContextInterface
)response
(\Liborm85\LoggableHttpClient\Context\ResponseContextInterface
)info
(\Liborm85\LoggableHttpClient\Context\InfoContextInterface
)
'Response content (canceled):'
(level:info
) - same as above, only for canceled request (not all data may be available)
Adds additional information to getInfo()
method:
request_json
(mixed
) -json
option from request$options
(if is provided)request_body
(\Liborm85\LoggableHttpClient\Body\BodyInterface
) -body
option from request$options
transformed to object (if is provided)response_time
(float
) - the time when the response was received
Usage
<?php $httpClient = \Symfony\Component\HttpClient\HttpClient::create(); // optional $loggableHttpClient = new \Liborm85\LoggableHttpClient\LoggableHttpClient($httpClient); $loggableHttpClient->setLogger(new \MyLogger()); $response = $loggableHttpClient->request('GET', 'https://example.com'); class MyLogger extends \Psr\Log\AbstractLogger { public function log($level, $message, array $context = []): void { if (isset($context['request']) && ($context['request'] instanceof \Liborm85\LoggableHttpClient\Context\RequestContextInterface)) { $context['request']->getContent(); // request content body as string (string)$context['request']; // is Stringable, request content body as string $context['request']->toStream(); // request content body as PHP stream $context['request']->getHeadersAsString(); // request headers as string $context['request']->getHeaders(); // request headers as array (string[][]) $context['request']->getRequestTime(); // request time as DateTimeInterface $context['request']->getRequestMethod(); // request HTTP method $context['request']->getUrl(); // full request URL } if (isset($context['response']) && ($context['response'] instanceof \Liborm85\LoggableHttpClient\Context\ResponseContextInterface)) { $context['response']->getContent(); // response content body as string (string)$context['response']; // is Stringable, response content body as string $context['response']->toStream(); // response content body as PHP stream $context['response']->getHeadersAsString(); // response headers as string $context['response']->getHeaders(); // response headers as array (string[][]) $context['response']->getResponseTime(); // response time as DateTimeInterface } if (isset($context['info']) && ($context['info'] instanceof \Liborm85\LoggableHttpClient\Context\InfoContextInterface)) { $context['info']->getInfo(); // return all available information $context['info']->getInfo('url'); // return one information for provided type } } }
License
MIT