anym0re / laravel-http-client-logger
Laravel HTTP 客户端的日志记录器
Requires
- php: ^7.4|^8.0
- guzzlehttp/guzzle: ^7.2
- illuminate/http: ^8.0
- illuminate/support: ^8.0
- spatie/laravel-package-tools: ^1.1
Requires (Dev)
- orchestra/testbench: ^6.0
- phpunit/phpunit: ^9.3
- spatie/laravel-ray: ^1.12
- timacdonald/log-fake: ^1.9
- vimeo/psalm: ^4.4
This package is not auto-updated.
Last update: 2024-09-28 09:39:03 UTC
README
一个简单而非常灵活的 Laravel HTTP 客户端日志记录器。
安装
您可以通过 composer 安装此包
composer require bilfeldt/laravel-http-client-logger
Laravel
此包利用了 Laravel 的包自动发现机制,因此无需执行任何其他步骤 - 直接跳转到下面的 使用 部分。如果您出于某种原因希望禁用包自动发现,请参阅 Laravel 文档 获取更多详细信息。
Lumen
注意:Lumen 并非此包的官方支持。然而,我们目前尚未发现任何不兼容性。
如果您使用 Lumen,请在 bootstrap/app.php
中注册服务提供程序,如下所示
<?php // bootstrap/app.php $app->register(Bilfeldt\LaravelHttpClientLogger\LaravelHttpClientLoggerServiceProvider::class); // If you want to use the Facades provided by the package $app->withFacades();
配置
可选地在 Laravel 中使用以下命令发布配置文件
php artisan vendor:publish --provider="Bilfeldt\LaravelHttpClientLogger\LaravelHttpClientLoggerServiceProvider" --tag="laravel-http-client-logger-config"
使用
使用此日志记录器将记录使用 Laravel HTTP 客户端进行的对外部 HTTP 请求的请求、响应和响应时间。
基本日志记录
Http::log()->get('https://example.com'); // uses the configured logger and filter
条件日志记录
这将当 $condition
评估为 true
时记录请求/响应。
Http::logWhen($condition)->get('https://example.com'); // uses the configured logger and filter
日志上下文
您可以使用以下方式为日志记录器提供上下文
Http::log(['note' => 'Something to log'])->get('https://example.com'); // or Http::logWhen($condition, ['note' => 'Something to log'])->get('https://example.com');
提供按需配置
您可以提供按需配置,这将覆盖在 config/laravel-http-client-logger.php
中指定的包配置
Http::log($context, ['example-config-key' => 'value'])->get('https://example.com'); // or Http::logWhen($condition, $context, ['example-config-key' => 'value'])->get('https://example.com');
指定日志记录器
默认日志记录器和过滤器分别由包配置中的 logger
和 filter
指定,但可以在运行时使用以下方式更改
Http::log($context, $config, $logger, $filter)->get('https://example.com'); // or Http::logWhen($condition, $context, $config, $logger, $filter)->get('https://example.com');
请注意,日志记录器必须实现 HttpLoggerInterface
,而过滤器必须实现 HttpLoggingFilterInterface
。
日志记录示例
默认日志记录器将请求和响应转换为 PSR-7 HTTP 消息,然后将其作为字符串记录。
使用默认日志记录器时的日志条目示例
Http::log()->get('https://repo.packagist.org/p2/bilfeldt/laravel-http-client-logger.json'); //[2021-03-08 06:58:49] local.DEBUG: Time 0.12105202674866sec //Request //GET /p2/bilfeldt/laravel-http-client-logger.json HTTP/1.1 //User-Agent: GuzzleHttp/7 //Host: repo.packagist.org // // //Response //HTTP/1.1 200 OK //Server: nginx //Date: Mon, 08 Mar 2021 06:58:49 GMT //Content-Type: application/json //Last-Modified: Wed, 17 Feb 2021 14:31:03 GMT //Transfer-Encoding: chunked //Connection: keep-alive //Vary: Accept-Encoding // //{"packages":{"bilfeldt/laravel-http-client-logger":[{"name":"bilfeldt/laravel-http-client-logger","description":"A logger for the Laravel HTTP Client","keywords":["bilfeldt","laravel-http-client-logger"],"homepage":"https://github.com/bilfeldt/laravel-http-client-logger","version":"v0.2.0","version_normalized":"0.2.0.0","license":["MIT"],"authors":[{"name":"Anders Bilfeldt","email":"abilfeldt@gmail.com","role":"Developer"}],"source":{"type":"git","url":"https://github.com/bilfeldt/laravel-http-client-logger.git","reference":"67ea252a3d3d0c9c0e1c7daa11a3683db818ad5e"},"dist":{"type":"zip","url":"https://api.github.com/repos/bilfeldt/laravel-http-client-logger/zipball/67ea252a3d3d0c9c0e1c7daa11a3683db818ad5e","reference":"67ea252a3d3d0c9c0e1c7daa11a3683db818ad5e","shasum":""},"type":"library","time":"2021-02-17T14:28:45+00:00","autoload":{"psr-4":{"Bilfeldt\\LaravelHttpClientLogger\\":"src"}},"extra":{"laravel":{"providers":["Bilfeldt\\LaravelHttpClientLogger\\LaravelHttpClientLoggerServiceProvider"]}},"require":{"php":"^7.4|^8.0","guzzlehttp/guzzle":"^7.2","illuminate/http":"^8.0","illuminate/support":"^8.0","spatie/laravel-package-tools":"^1.1"},"require-dev":{"orchestra/testbench":"^6.0","phpunit/phpunit":"^9.3","spatie/laravel-ray":"^1.12","timacdonald/log-fake":"^1.9","vimeo/psalm":"^4.4"},"support":{"issues":"https://github.com/bilfeldt/laravel-http-client-logger/issues","source":"https://github.com/bilfeldt/laravel-http-client-logger/tree/v0.2.0"}},{"version":"0.1.0","version_normalized":"0.1.0.0","source":{"type":"git","url":"https://github.com/bilfeldt/laravel-http-client-logger.git","reference":"6bb8c8ada3959643103a75aa4e639c8dddddf2df"},"dist":{"type":"zip","url":"https://api.github.com/repos/bilfeldt/laravel-http-client-logger/zipball/6bb8c8ada3959643103a75aa4e639c8dddddf2df","reference":"6bb8c8ada3959643103a75aa4e639c8dddddf2df","shasum":""},"time":"2021-02-15T22:39:05+00:00","support":{"issues":"https://github.com/bilfeldt/laravel-http-client-logger/issues","source":"https://github.com/bilfeldt/laravel-http-client-logger/tree/0.1.0"}}]},"minified":"composer/2.0"}
测试
composer test
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG
鸣谢
- Anders Bilfeldt:主要包开发者
- Henry Be:封面图像
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件