webclient / ext-log
Logging extension for PSR-18 HTTP client
Fund package maintenance!
www.paypal.me/ddrv
Requires
- php: >=7.0
- psr/http-client: ^1.0
- psr/log: ^1.0
Requires (Dev)
- guzzlehttp/psr7: ^1.7
- phpunit/phpunit: >=6.5
- squizlabs/php_codesniffer: ^3.5
- webclient/fake-http-client: ^1.0
Suggests
- psr/http-client-implementation: Choice your favorite psr-18 implementation
- psr/log-implementation: Choice your favorite psr-3 implementation
Provides
This package is auto-updated.
Last update: 2024-11-04 19:17:17 UTC
README
webclient/ext-log
Logging decorator for PSR-18 HTTP clients.
Install
Install this package, your favorite psr-3 implementation and your favorite psr-18 implementation.
composer require webclient/ext-log:^1.0
Using
<?php use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; use Psr\Log\LoggerInterface; use Webclient\Extension\Log\Client; /** * @var ClientInterface $client * @var LoggerInterface $logger */ $http = new Client($client, $logger); /** @var RequestInterface $request */ $response = $http->sendRequest($request);
Custom Request ID
You may implement \Webclient\Extension\Log\IdGenerator\IdGenerator
for your Request ID in logs
<?php use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; use Psr\Log\LoggerInterface; use Webclient\Extension\Log\Client; use Webclient\Extension\Log\IdGenerator\IdGenerator; /** * @var ClientInterface $client * @var LoggerInterface $logger * @var IdGenerator $idGenerator */ $http = new Client($client, $logger, $idGenerator); /** @var RequestInterface $request */ $response = $http->sendRequest($request);
Custom log output
You may implement \Webclient\Extension\Log\Formatter\Formatter
for your Request/Response output in logs
<?php use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; use Psr\Log\LoggerInterface; use Webclient\Extension\Log\Client; use Webclient\Extension\Log\Formatter\Formatter; /** * @var ClientInterface $client * @var LoggerInterface $logger * @var Formatter $formatter */ $http = new Client($client, $logger, null, $formatter); /** @var RequestInterface $request */ $response = $http->sendRequest($request);
Log levels
You may set your log levels
<?php use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestInterface; use Psr\Log\LoggerInterface; use Psr\Log\LogLevel; use Webclient\Extension\Log\Client; /** * @var ClientInterface $client * @var LoggerInterface $logger */ $http = new Client( $client, $logger, null, null, LogLevel::INFO, // Request log level LogLevel::INFO, // Info responses (status codes 1xx) LogLevel::INFO, // Success responses (status codes 2xx) LogLevel::INFO, // Redirect responses (status codes 3xx) LogLevel::EMERGENCY, // Client error responses (status codes 4xx) LogLevel::ERROR, // Server error responses (status codes 5xx) LogLevel::WARNING // Base HTTP client exceptions ); /** @var RequestInterface $request */ $response = $http->sendRequest($request);