bilfeldt / laravel-http-client-logger
Laravel HTTP 客户端记录器
v2.2.0
2024-02-28 09:35 UTC
Requires
- php: ~8.3.0 | ~8.2.0 | ~8.1.0
- ext-json: *
- guzzlehttp/guzzle: ^7.2
- illuminate/http: ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^9.0 || ^10.0 || ^11.0
- spatie/laravel-package-tools: ^1.1
Requires (Dev)
- orchestra/testbench: ^7.0 || ^8.0 || ^9.0
- phpunit/phpunit: ^9.5.10 || ^10.0
- spatie/laravel-ray: ^1.29
- timacdonald/log-fake: ^2.0
- vimeo/psalm: ^4.20
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="http-client-logger-config"
使用
使用记录器将记录外部 HTTP 请求的请求、响应和响应时间,这些请求是用 Laravel 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'); // or the shorthand syntax which will use the LogAllFilter Http::logWith($logger)->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)。请参阅 许可文件 了解更多详细信息。