dittto / symfony-request-logger
通过guzzle处理外部请求的日志
Requires (Dev)
- guzzlehttp/guzzle: ~6.0
- phpunit/phpunit: ^5.7
- psr/log: ~1.0
- symfony/dependency-injection: ^3.0
- symfony/symfony: ~3.3
This package is not auto-updated.
Last update: 2024-09-29 05:27:38 UTC
README
这是什么?
我们编写的任何应用程序都可能会经常需要调用我们的或第三方API。这个Symfony扩展包使得跟踪这些请求所需时间并返回结果变得简单,无论是通过monolog还是通过在Symfony的调试模式下附加到输出中的额外JSON对象。
它通过向Guzzle添加额外的中间件来工作,以便我们可以跟踪请求何时被发起,以及请求是成功还是失败,然后将这些日志存储起来以供以后输出。
如何设置它
基本设置
使用这个请求日志记录器有几个不同的选项。最简单的是记录通过Guzzle发出的所有请求,然后在以后召回日志。
为此,请添加/更新以下服务。如果您已经有了Guzzle服务,则根据需要更新它
services: dittto.request_logger.alias: alias: 'dittto.request_logger' http_client: class: GuzzleHttp\Client arguments: - handler: '@http_client.handlerstack' connect_timeout: 5 timeout: 5 http_client.handlerstack: class: GuzzleHttp\HandlerStack factory: [ GuzzleHttp\HandlerStack, 'create' ] calls: - [ 'push', [ '@dittto.request_logger.middleware.request' ] ]
这会做什么呢?第一个服务是定义我们将使用哪个版本的请求日志记录器。dittto.request_logger
是LoggerInterface
的最基本形式,它只是简单地存储所有请求以供以后使用。
之后,我们创建一个Guzzle客户端,并确保我们的中间件已被添加,这样我们就可以存储我们的API请求尝试。
要使用它,您可能需要做类似以下操作
services: test_controller: class: AppBundle\Controller\TestController arguments: [ '@http_client', '@dittto.request_logger.alias' ]
<?php class TestController { private $client; private $logger; public function __construct(ClientInterface $client, RetrievableLogsInterface $logger) { $this->client = $client; $this->logger = $logger; } public function index() { $this->client->request('GET', 'https://api-path'); var_dump($this->logger->getLogs()); } }
使用monolog
上面显示了如何使用它并自行处理结果。实际上,您可能希望以某种方式自动输出请求日志。
我们将使用monolog来输出我们的日志消息。首先,让我们为所有这些消息创建一个唯一的monolog通道
monolog: channels: [ "external_request" ]
接下来,创建以下服务。如果您已经定义了Guzzle,则只需选择您希望使用的设置
services: dittto.request_logger.alias: alias: 'dittto.request_logger.passthrough' dittto.request_logger.monolog_channel: alias: 'monolog.logger.external_request' http_client: class: GuzzleHttp\Client arguments: - handler: '@http_client.handlerstack' connect_timeout: 5 timeout: 5 http_client.handlerstack: class: GuzzleHttp\HandlerStack factory: [ GuzzleHttp\HandlerStack, 'create' ] calls: - [ 'push', [ '@dittto.request_logger.middleware.request' ] ]
注意,这次的变化是针对前两个服务的。别名现在引用dittto.request_logger.passthrough
,这允许我们将日志消息同时保存到请求日志记录器和monolog。
第二个服务是引用上面创建的新monolog通道的别名。
现在,如果您运行代码,monolog将保存更改,请求日志记录器通过getLogs()
可用。
如果您只是打算使用monolog记录这些消息,则可以使用Guzzle中间件而不是其他所有内容。这是使用以下服务完成的
services: http_client: class: GuzzleHttp\Client arguments: - handler: '@http_client.handlerstack' connect_timeout: 5 timeout: 5 http_client.handlerstack: class: GuzzleHttp\HandlerStack factory: [ GuzzleHttp\HandlerStack, 'create' ] calls: - [ 'push', [ '@dittto.request_logger.middleware.request.monolog_only' ] ] dittto.request_logger.middleware.request.monolog_only: class: Closure factory: [ '@dittto.request_logger.middleware', 'onRequest' ] arguments: [ '@monolog.logger.external_request' ]
JSON调试对象
如果您正在构建、维护和调试大量的JSON API,那么当Symfony处于调试模式时,这个插件可以自动将您的请求日志添加到您的JSON输出中。
将以下服务添加到您的应用程序中,以捕获有效的响应并更新您的JSON
services: dittto.request_logger.debug_listener: class: Dittto\RequestLoggerBundle\Listener\JSONDebugListener arguments: [ '@dittto.request_logger', '%kernel.debug%' ] tags: - { name: kernel.event_listener, event: kernel.response, method: 'onKernelResponse' }