anik / loguzz
Loguzz 是一个 guzzlehttp/guzzle 请求和响应日志记录器
Requires
- php: ^8.0
- guzzlehttp/guzzle: ^6.2|^7.1
- guzzlehttp/promises: ^1.5|^2.0
- psr/log: ^2.0|^3.0
Requires (Dev)
- colinodell/psr-testlogger: ^1.2
- phpunit/phpunit: ^9.0|^10.0
README
Loguzz

Loguzz 是一个用于 Guzzle 的中间件,用于记录请求和响应。
安装
您需要使用 composer 来安装此包。 composer require anik/loguzz
文档 V1
详细的文档可以在这里找到。
文档 V4/V3/V2
要记录请求,您需要将 Loguzz\Middleware\LogMiddleware
推送到 Guzzle 的处理器。
$logger = new \ColinODell\PsrTestLogger\TestLogger(); $handlerStack = \GuzzleHttp\HandlerStack::create(); $options = []; $handlerStack->push(new \Loguzz\Middleware\LogMiddleware($logger, $options), 'logger');
$logger
是Psr\Log\LoggerInterface
的实现。$options
是一个数组,用于更改 LogMiddleware 的默认行为。'logger'
是 Guzzle 中间件的内部名称。它可以是任何名称。
选项
// Default values $options = [ 'length' => 100, 'log_request' => true, 'log_response' => true, 'success_only' => false, 'exceptions_only' => false, 'log_level' => 'debug', 'request_formatter' => new \Loguzz\Formatter\RequestCurlFormatter(), 'response_formatter' => new \Loguzz\Formatter\ResponseJsonFormatter(), 'exception_formatter' => new \Loguzz\Formatter\ExceptionJsonFormatter(), 'tag' => '', 'force_json' => true, 'separate' => false, ];
length
- int。最小值为 10。用于设置使用\Loguzz\Formatter\RequestCurlFormatter
格式化请求时的长度。log_request
- bool。用于启用或禁用请求日志记录。log_response
- bool。用于启用或禁用响应日志记录。success_only
- bool。仅记录成功的响应。如果服务器可以到达,则视为成功。exception_only
- bool。仅记录异常。记录 Guzzle 在连接/超时相关异常时抛出的异常。log_level
- string。任何有效的日志级别。request_formatter
- \Loguzz\Formatter\AbstractRequestFormatter 的实例。可用\Loguzz\Formatter\RequestArrayFormatter
\Loguzz\Formatter\RequestCurlFormatter
\Loguzz\Formatter\RequestJsonFormatter
response_formatter
- \Loguzz\Formatter\AbstractResponseFormatter 的实例\Loguzz\Formatter\ResponseArrayFormatter
\Loguzz\Formatter\ResponseJsonFormatter
exception_formatter
- \Loguzz\Formatter\AbstractResponseFormatter 的实例\Loguzz\Formatter\ExceptionArrayFormatter
\Loguzz\Formatter\ExceptionJsonFormatter
tag
- string。默认为 Empty。如果为非空字符串,则将在该标签下记录格式化后的数据。标签可用于在日志文件或存储中搜索特定类型的请求/响应。force_json
- bool。默认为 true。仅当 tag 为非空字符串时适用。如果启用,它将记录数据为 JSON 字符串,否则将记录为数组。如果设置为false
,由于 psr/log 接口中的类型提示,代码可能会损坏。如果您的 logger interface 支持 array,则它将正常工作。separate
- 布尔值。仅在 标签 为非空字符串时适用。如果启用,则会在{tag}.request
、{tag}.success
和{tag}.failure
中记录请求日志、成功响应和错误响应。
请求格式化器
要创建一个新的请求格式化器,需要 扩展 \Loguzz\Formatter\AbstractRequestFormatter
类。
响应格式化器
要创建一个新的响应格式化器,需要 扩展 \Loguzz\Formatter\AbstractResponseFormatter
类。
异常格式化器
要创建一个新的异常格式化器,需要 扩展 \Loguzz\Formatter\AbstractExceptionFormatter
类。
手动请求格式化
\Loguzz\Formatter\AbstractRequestFormatter::format
的实现接受以下参数
\Psr\Http\Message\RequestInterface $request
array $options = []
可用的请求格式化器从 $request
和 $options
中的 cookies 解析数据。在 $options['cookies']
中的值必须是 \GuzzleHttp\Cookie\CookieJarInterface
的实现。为了解析 cookies,请求 URL 必须包含 域名。
手动响应格式化
\Loguzz\Formatter\AbstractResponseFormatter::format
的实现接受以下参数
\Psr\Http\Message\RequestInterface $request
\Psr\Http\Message\ResponseInterface $response
array $options = []
可用的响应格式化器从 $response
和 set-cookie
头中的 cookies 解析数据。为了解析 cookies,请求 URL 必须包含 域名。
对于可用的响应格式化器,set-cookie
头将不可用。
问题 & PR
如果您认为包中缺少某些内容或引起错误,请报告问题。如果您有空闲时间和想为仓库做出贡献,请提交 PR。