softonic/guzzle_logger
Composer 安装命令:
composer require softonic/guzzle_logger
包简介
A Guzzle middleware to log request and responses automatically
README 文档
README
This is a middleware for guzzle that will help you automatically log every request and response using a PSR-3 logger.
The middleware is functional with version 6 of Guzzle.
Install
Via Composer
$ composer require softonic/guzzle_logger
Usage
Simple usage
use GuzzleLogMiddleware\LogMiddleware; use GuzzleHttp\HandlerStack; $logger = new Logger(); //A new PSR-3 Logger like Monolog $stack = HandlerStack::create(); // will create a stack stack with middlewares of guzzle already pushed inside of it. $stack->push(new LogMiddleware($logger)); $client = new GuzzleHttp\Client([ 'handler' => $stack, ]);
From now on each request and response you execute using $client object will be logged.
By default the middleware logs every activity with level DEBUG.
Advanced initialization
The signature of the LogMiddleware class is the following:
\GuzzleLogMiddleware\LogMiddleware( \Psr\Log\LoggerInterface $logger, \GuzzleLogMiddleware\Handler\HandlerInterface $handler = null, bool $onFailureOnly = false, bool $logStatistics = false );
- logger - The PSR-3 logger to use for logging.
- handler - A HandlerInterface class that will be responsible for logging your request/response. Check Handlers sections.
- onFailureOnly - By default the middleware is set to log every request and response. If you wish to log the HTTP messages only when guzzle returns a rejection set this as true or when an exception occurred. Guzzle returns a rejection when http_errors option is set to true.
- logStatistics - If you set this option as true then the middleware will also log statistics about the HTTP transaction.
Handlers
In order to make the middleware more flexible we allow the developer to initialize it with a handler.
A handler is the class that will be responsible for logging the HTTP message and it must implement a HandlerInterface.
As an example let's say that we create the following handler:
<?php namespace GuzzleLogMiddleware\Handler; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\RequestInterface; use GuzzleHttp\TransferStats; use Psr\Log\LoggerInterface; /** A simple handler that logs only requests */ final class SimpleHandler implements HandlerInterface { public function log( LoggerInterface $logger, RequestInterface $request, ?ResponseInterface $response = null, ?\Throwable $exception = null, ?TransferStats $stats = null, array $options = [] ): void { $logger->debug('Guzzle HTTP request: ' . \GuzzleHttp\Psr7\str($request)); return; } }
We can pass the handler above during construction of the middleware.
<?php use GuzzleLogMiddleware\LogMiddleware; use GuzzleHttp\HandlerStack; $logger = new Logger(); //A new PSR-3 Logger like Monolog $stack = HandlerStack::create(); // will create a stack stack with middlewares of guzzle already pushed inside of it. $stack->push(new LogMiddleware($logger, new SimpleHandler())); $client = new GuzzleHttp\Client([ 'handler' => $stack, ]);
From now on all Requests will be logged. Note that at the example above only requests are logged.
Important
If no handler is passed the middleware will initialize it's own handler. At the moment the default one is MultiRecordArrayHandler
MultiRecordArrayHandler
This is the default handler used from the middleware. This handler uses internally the FixedStrategy and logs all request
and responses with level debug. This handler adds a separate log entry for each Request, Response, Exception or TransferStats.
The information about each object are added as a context array to the log entry.
StringHandler
This handler uses internally the FixedStrategy and logs all request and responses with level debug. You can initialize this handler
with a custom strategy. This handler adds a separate log entry for each Request, Response, Exception or TransferStats.
The handler converts the objects to strings and the information about each object are added to the message of the log entry.
Log Level Strategies
Strategies are used to define the LogLevel that the handler will use to log each object.
FixedStrategy
You can use this strategy to log each HTTP Message with a specific level.
StatusCodeStrategy
You can use this strategy to log each HTTP Response with a specific level depending on the status code of the Response.
$strategy = new StatusCodeStrategy( LogLevel::INFO, // Default level used for requests or for responses that status code are not set with a different level. LogLevel::CRITICAL // Default level used for exceptions. ); $strategy->setLevel(404, LogLevel::WARNING); $multiRecordArrayHandler = new MultiRecordArrayHandler($strategy); $logger = new Logger(); //A new PSR-3 Logger like Monolog $stack = HandlerStack::create(); // will create a stack stack with middlewares of guzzle already pushed inside of it. $stack->push(new LogMiddleware($logger, $multiRecordArrayHandler)); $client = new GuzzleHttp\Client([ 'handler' => $stack, ]);
Using options on each request
You can set on each request options about your log.
$client->get('/', [ 'log' => [ 'on_exception_only' => true, 'statistics' => true, ] ]);
on_exception_onlyDo not log anything unless if the response status code is above the threshold.statisticsif theon_exception_onlyoption/variable is true and this is also true the middleware will log statistics about the HTTP call.
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Credits
License
The MIT License (MIT). Please see License File for more information.
softonic/guzzle_logger 适用场景与选型建议
softonic/guzzle_logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.59k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 05 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「log」 「logging」 「Guzzle」 「PSR3」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 softonic/guzzle_logger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 softonic/guzzle_logger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 softonic/guzzle_logger 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
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).
Laravel 5.x.x library for integration Monolog Sentry
High-performance, zero-dependency PSR-3 logger for MonkeysLegion with structured logging, handlers, processors, and PHP 8.4 features.
Pacote simplificado para persistência de logs
统计信息
- 总下载量: 3.59k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-05-06