garbetjie/http-request-logger
Composer 安装命令:
composer require garbetjie/http-request-logger
包简介
A request logger that can log all incoming & outgoing requests and responses.
README 文档
README
An HTTP request logging library that makes it simple to easy to implement the logging of all HTTP requests & responses.
Works with Laravel, Guzzle and PHP's SoapServer and
SoapClient classes.
Table of contents
Introduction
It is often quite useful to be able to see all requests & responses generated by your application - especially whilst in development. This library makes it trivial to log all incoming & outgoing requests & responses using Monolog.
By default, all requests & responses are logged at the debug log level. Sensible defaults for logging context are used,
and headers that may contain sensitive values (such as Cookie, Set-Cookie and Authorization) have their values
obfuscated. Each request & corresponding response are linked together through a unique ID. This allows you to interrogate
your logs to find the matching response for any given request.
Installation
composer require garbetjie/http-request-logger
Usage
Incoming requests in Laravel
Enabling request logging in Laravel is as simple as adding the middleware to the $middleware property on your
App\Http\Kernel class. The example shown below is enough to ensure that all incoming requests & outgoing responses are
logged in Laravel:
<?php namespace App\Http; use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel { // ... protected $middleware = [ \Garbetjie\RequestLogging\Http\Middleware\IncomingRequestLoggingMiddleware::class ]; // ... }
Take note: You should add the request logging middleware to your application's global middleware stack, and determine when logging should take place in a service provider. When added to a middleware group, any uncaught exceptions thrown will prevent the request & response from being logged.
See the Toggle enabled/disabled section for more information on this.
Incoming requests in PSR-15 compliant frameworks
If you are not using Laravel, but are making use of a framework that adheres to PSR-15, you'll need to be a little more explicit in creating the middleware. The example below makes use of the Slim framework as an example:
<?php $app = Slim\Factory\AppFactory::create(); $app->add(\Garbetjie\RequestLogging\Http\Middleware\IncomingRequestLoggingMiddleware::class); $app->run();
Incoming SOAP requests
Logging incoming SOAP requests is trivial. Instead of creating a new \SoapServer instance, simply create a new instance
of \Garbetjie\RequestLogging\Http\SoapServer. You can use this new instance exactly the same as you would with an
instance of \SoapServer. Example shown below:
<?php $monolog = new Monolog\Logger('logger'); $logger = new Garbetjie\RequestLogging\Http\Logger($monolog); $server = new Garbetjie\RequestLogging\Http\SoapServer($logger, '/path/to/wsdl/or/null'); $server->setObject(new stdClass()); $server->handle();;
Outgoing requests in Laravel
WHen using Laravel, you can simply type-hint either GuzzleHttp\ClientInterface or GuzzleHttp\Client wherever
Laravel performs dependency injection. This package includes a service provider that automatically adds the middleware
to a handler stack for you.
An example of using this type-hinting in a controller is shown below:
<?php namespace App\Http\Controllers; class MyController { public function myAction(\GuzzleHttp\ClientInterface $client) { return $client->request('GET', 'https://example.org')->getBody()->getContents(); } }
Outgoing requests through Guzzle
Outbound request logging happens through Guzzle middleware. In order to log outbound requests, simply add an instance
of the OutgoingRequestLoggingMiddleware to your handler stack. Ideally, this should be the last middleware in your
handler stack in order to ensure the logged representation of the request is as accurate as possible.
<?php $monolog = new Monolog\Logger('logger'); $logger = new Garbetjie\RequestLogging\Http\Logger($monolog); $stack = GuzzleHttp\HandlerStack::create(); $stack->push(new Garbetjie\RequestLogging\Http\Middleware\OutgoingRequestLoggingMiddleware($logger)); $client = new GuzzleHttp\Client(['stack' => $stack]); $client->request('GET', 'https://example.org');
Outgoing SOAP requests
In order to log outbound SOAP requests, simply create your Guzzle client as if you be logging any other outgoing HTTP
request, and pass this client instance to a new instance of Garbetjie\RequestLogging\Http\SoapClient. You can use
this SOAP client instance just as if you were making use of a native SoapClient instance:
<?php /* @var GuzzleHttp\Client $guzzleClient */ $soapClient = new Garbetjie\RequestLogging\Http\SoapClient($guzzleClient, null, []); $soapResponse = $soapClient->MyCustomSoapMethod(['parameters']);
Customisation
All aspects of the request & response logging are customisable. This includes the ID used to link a request & response together, the log message, as well as the context logged with each request & response.
Logging context
By default, incoming & outgoing requests generate a context with a similar structure to below.
Sensitive headers such as Authorization and Cookie have their values replaced with ***:
<?php $context = [ 'id' => '', // string - contains the unique ID that links this request to its response. 'method' => '', // string - upper-cased request method. 'url' => '', // string - URL to which the request was sent. 'body_length' => 0, // integer - size of the body sent. 'body' => base64_encode(''), // string - base64-encoded body sent in the request. 'headers' => [], // array<string, string> - array of headers sent in the request. Names are normalized and lower-cased. ];
The context shown below indicates the default structure of the logging context for responses.
Any Set-Cookie headers have their values stripped out and replaced with ***.
<?php $context = [ 'id' => '', // string - contains the unique ID that links this response to the request that created it. 'duration' => 0.0, // float - the duration of the request, in seconds (with fractional milliseconds). 'status_code' => 0, // integer - the HTTP status code returned in the response. 'body_length' => 0, // integer - the size of the body sent. 'body' => base64_encode(''), // string - base64-encoded body sent in the response. 'headers' => [], // array<string, string> - array of headers sent in the response. Names are normalized and lower-cased. ];
It is quite simple to customise the logging context that is generated:
<?php $monolog = new Monolog\Logger('name'); $logger = new Garbetjie\RequestLogging\Http\Logger($monolog); $logger->context( function (Garbetjie\RequestLogging\Http\RequestEntry $entry): array { // Return an array containing the context to log. return []; }, function (Garbetjie\RequestLogging\Http\ResponseEntry $entry): array { // Return an array containing the context to log. return []; } );
If you'd like to simply extend off the context that is created by default, you can reuse the context extractors that are already available:
<?php $monolog = new Monolog\Logger('name'); $logger = new Garbetjie\RequestLogging\Http\Logger($monolog); $logger->context( function (Garbetjie\RequestLogging\Http\RequestEntry $entry): array { $context = (new Garbetjie\RequestLogging\Http\Context\RequestContext())($entry); $body = base64_decode($context['body']); // Modify $body. $context['body'] = base64_encode($body); return $context; }, null );
ID generation.
By default, the ID generation for linking requests & responses together makes use of endyjasmi/cuid to generate a full
CUID.
This can be easily customised by providing a callable that simply returns a string containing the ID to use:
<?php $monolog = new Monolog\Logger('name'); $logger = new Garbetjie\RequestLogging\Http\Logger($monolog); $logger->id( function(): string { return base64_encode(random_bytes(8)); } );
Toggling logging
By default, all requests & responses are logged. However, it is possible to toggle whether a request or response should be logged. Simply provide either a boolean value, or a callable that returns a boolean value indicating whether or not logging should be enabled for the given request:
<?php $monolog = new Monolog\Logger('name'); $logger = new Garbetjie\RequestLogging\Http\Logger($monolog); function shouldLog($request) { if ($request instanceof Symfony\Component\HttpFoundation\Request) { return stripos($request->getUri(), 'https://example.org') === false; } elseif ($request instanceof Psr\Http\Message\RequestInterface) { return stripos((string)$request->getUri(), 'https://example.org') === false; } else { return false; } } $logger->enabled( function(Garbetjie\RequestLogging\Http\RequestEntry $entry): bool { // Provide a callable that returns a boolean. return shouldLog($entry->request()); }, function(Garbetjie\RequestLogging\Http\ResponseEntry $entry): bool { return shouldLog($entry->request()); } ); // Alternatively: $logger->enabled(false, true); $logger->enabled( new Garbetjie\RequestLogging\Http\Context\RequestContext(), new Garbetjie\RequestLogging\Http\Context\ResponseContext() );
Changelog
See CHANGELOG.md for the full changelog.
garbetjie/http-request-logger 适用场景与选型建议
garbetjie/http-request-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.85k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2019 年 10 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「logging」 「http」 「Guzzle」 「request」 「laravel」 「psr」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 garbetjie/http-request-logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 garbetjie/http-request-logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 garbetjie/http-request-logger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
A Zend Framework module that sets up Monolog for logging in applications.
Asynchronous Sentry for Symfony - Fire and forget
Stackdriver handler for Monolog (codeinternetapplications/monolog-stackdriver Fork).
High-performance, zero-dependency PSR-3 logger for MonkeysLegion with structured logging, handlers, processors, and PHP 8.4 features.
Cloudflare Middleware For Guzzle
统计信息
- 总下载量: 1.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-10-22