定制 lightools/bitbang-http-logger 二次开发

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

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

lightools/bitbang-http-logger

Composer 安装命令:

composer require lightools/bitbang-http-logger

包简介

Logger for HTTP requests and responses performed via bitbang/http library.

README 文档

README

This library allows you to log all HTTP traffic performed via clients in bitbang/http library.

Installation

$ composer require lightools/bitbang-http-logger

Basic usage

$writer = new Lightools\BitbangLogger\Writers\DefaultWriter(__DIR__ . '/logs/http');
$logger = new Lightools\BitbangLogger\HttpLogger($writer);

$client = new Bitbang\Http\Clients\CurlClient();
$client->onRequest([$logger, 'onRequest']);
$client->onResponse([$logger, 'onResponse']);

$url = 'https://maps.googleapis.com/maps/api/distancematrix/json?origins=Praha&destinations=Brno';
$request = new Bitbang\Http\Request(Bitbang\Http\Request::GET, $url);

try {
    $client->process($request);
} catch (Bitbang\Http\BadResponseException $e) {
    // process exception
}

Logging

Example above will log into file ./logs/http/2016-03-22/17-25-00-44956900_56fa569cc8c8d.txt (contents shown below). You can see that request and response are in the same file and some timestamps are included above both HTTP messages. This behavior is done by DefaultWriter and you can easily change it by implementing own IWritter (you may want to save HTTP request and response separately, change the file naming or store logs in database). DefaultWriter also saves whole HTTP communication when some redirect occurs - all subsequent HTTP requests will be stored in one file.

(17-25-00-44956900)
GET https://maps.googleapis.com/maps/api/distancematrix/json?origins=Praha&destinations=Brno HTTP/1.1
expect:
connection: keep-alive
user-agent: Bitbang/0.3.0 (cURL)


(17-25-00-77731500)
HTTP/1.1 200
content-type: application/json; charset=UTF-8
date: Tue, 22 Mar 2016 16:25:27 GMT
expires: Wed, 23 Mar 2016 16:25:27 GMT
cache-control: public, max-age=86400
content-encoding: gzip
server: mafe
x-xss-protection: 1; mode=block
x-frame-options: SAMEORIGIN
alternate-protocol: 443:quic,p=1
alt-svc: quic=":443"; ma=2592000; v="31,30,29,28,27,26,25"
transfer-encoding: chunked

{
   "destination_addresses" : [ "Brno, Česká republika" ],
   "origin_addresses" : [ "Praha, Česká republika" ],
   "rows" : [
      {
         "elements" : [
            {
               "distance" : {
                  "text" : "205 km",
                  "value" : 205461
               },
               "duration" : {
                  "text" : "2 hod, 0 min",
                  "value" : 7217
               },
               "status" : "OK"
            }
         ]
      }
   ],
   "status" : "OK"
}

Formatting

By default, HTTP body is printed to log as it was set to Request or Response object, but you can register formatters to make it prettier and more readable. All you need is to implement IFormatter interface and register it in logger. This library is shipped with few handy formatters - for array (typical POST), JSON, XML or for urlencoded HTTP body. ArrayFormatter also supports quite common CURLFile.

The first formatter that is able to format HTTP message is used, so theoretically it may matter in which order you register them. Here is example how to use all default formatters:

$xmlLoader = new Lightools\Xml\XmlLoader();
$arrayDumper = new Lightools\BitbangLogger\ArrayDumper();

$arrayFormatter = new Lightools\BitbangLogger\Formatters\ArrayFormatter($arrayDumper);
$urlFormatter = new Lightools\BitbangLogger\Formatters\UrlEncodedFormatter($arrayDumper);
$jsonFormatter = new Lightools\BitbangLogger\Formatters\JsonFormatter(); // requires nette/utils to work
$xmlFormatter = new Lightools\BitbangLogger\Formatters\XmlFormatter($xmlLoader); // requires lightools/xml to work

$writer = new Lightools\BitbangLogger\Writers\DefaultWriter(__DIR__ . '/logs/http');
$logger = new Lightools\BitbangLogger\HttpLogger($writer);
$logger->registerFormatter($arrayFormatter);
$logger->registerFormatter($urlFormatter);
$logger->registerFormatter($jsonFormatter);
$logger->registerFormatter($xmlFormatter);

Limitations

Logged HTTP headers may not contain all really sent headers because some of them might be modified by proxies or even by curl library itself (e.g. very handy CURLOPT_ENCODING).

Multiple HTTP clients

If you are using bitbang clients in more services (for example lightools/fio and lightools/ares), and you don't want to store all logs in the same folder, custom IWriter or some HttpLoggerFactory and HttpClientFactory might be handy.

How to run tests

$ vendor/bin/tester tests

lightools/bitbang-http-logger 适用场景与选型建议

lightools/bitbang-http-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.31k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 03 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 lightools/bitbang-http-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lightools/bitbang-http-logger 我们能提供哪些服务?
定制开发 / 二次开发

基于 lightools/bitbang-http-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-03-29