定制 liborm85/loggable-http-client 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

liborm85/loggable-http-client

最新稳定版本:2.2.1

Composer 安装命令:

composer require liborm85/loggable-http-client

包简介

Extended logging for the Symfony HTTP Client.

README 文档

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

Installation

You can install it with:

composer require liborm85/loggable-http-client 

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'); $response->getContent(); // standard response content as string $response->toArray(); // standard response content as array $requestBody = $response->getInfo('request_body'); if (isset($requestBody) && ($requestBody instanceof \Liborm85\LoggableHttpClient\Body\BodyInterface)) { $requestBody->getContent(); // request content body as string (string)$requestBody; // is Stringable, request content body as string $requestBody->toStream(); // request content body as PHP stream $requestBody->toArray(); // request content body as array (if is possible, otherwise null) } $response->getInfo('response_time'); // the time when the response was received 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']->toArray(); // request content body as array (if is possible, otherwise null) $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']->toArray(); // response content body as array (if is possible, otherwise null) $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

统计信息

  • 总下载量: 8.53k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固