liborm85 / loggable-http-client
为 Symfony HTTP Client 增强日志记录。
2.1.0
2024-01-31 06:42 UTC
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
为 Symfony HTTP Client 增强日志记录,特别允许记录请求和响应的内容。
向 PSR-3 日志接口添加事件
'Response content:'
(级别:info
) - 接收到的响应体内容;在上下文中可用(详情见 用法)request
(\Liborm85\LoggableHttpClient\Context\RequestContextInterface
)response
(\Liborm85\LoggableHttpClient\Context\ResponseContextInterface
)info
(\Liborm85\LoggableHttpClient\Context\InfoContextInterface
)
'Response content (canceled):'
(级别:info
) - 与上面相同,仅针对取消的请求(可能不提供所有数据)
向 getInfo()
方法添加附加信息
request_json
(mixed
) - 请求$options
中的json
选项(如果提供)request_body
(\Liborm85\LoggableHttpClient\Body\BodyInterface
) - 请求$options
中的body
选项转换为对象(如果提供)response_time
(float
) - 接收到响应的时间
用法
<?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 } } }
许可证
MIT