[HttpClient] logger integration
This commit is contained in:
parent
4dfb741330
commit
fc6ba7efad
@ -11,6 +11,8 @@
|
||||
|
||||
namespace Symfony\Component\HttpClient;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Component\HttpClient\Exception\TransportException;
|
||||
use Symfony\Component\HttpClient\Response\CurlResponse;
|
||||
use Symfony\Component\HttpClient\Response\ResponseStream;
|
||||
@ -34,6 +36,7 @@ final class CurlHttpClient implements HttpClientInterface
|
||||
|
||||
private $defaultOptions = self::OPTIONS_DEFAULTS;
|
||||
private $multi;
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* @param array $defaultOptions Default requests' options
|
||||
@ -41,8 +44,10 @@ final class CurlHttpClient implements HttpClientInterface
|
||||
*
|
||||
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options
|
||||
*/
|
||||
public function __construct(array $defaultOptions = [], int $maxHostConnections = 6)
|
||||
public function __construct(array $defaultOptions = [], LoggerInterface $logger = null, int $maxHostConnections = 6)
|
||||
{
|
||||
$this->logger = $logger ?? new NullLogger();
|
||||
|
||||
if ($defaultOptions) {
|
||||
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, self::OPTIONS_DEFAULTS);
|
||||
}
|
||||
@ -86,6 +91,7 @@ final class CurlHttpClient implements HttpClientInterface
|
||||
*/
|
||||
public function request(string $method, string $url, array $options = []): ResponseInterface
|
||||
{
|
||||
$this->logger->notice('Making a request', ['url' => $url, 'method' => $method, 'client' => static::class]);
|
||||
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);
|
||||
$scheme = $url['scheme'];
|
||||
$authority = $url['authority'];
|
||||
@ -103,6 +109,7 @@ final class CurlHttpClient implements HttpClientInterface
|
||||
];
|
||||
|
||||
if ('GET' === $method && !$options['body'] && $expectedHeaders === $pushedHeaders) {
|
||||
$this->logger->debug('Creating pushed response');
|
||||
// Reinitialize the pushed response with request's options
|
||||
$pushedResponse->__construct($this->multi, $url, $options);
|
||||
|
||||
@ -156,7 +163,7 @@ final class CurlHttpClient implements HttpClientInterface
|
||||
// DNS cache removals require curl 7.42 or higher
|
||||
// On lower versions, we have to create a new multi handle
|
||||
curl_multi_close($this->multi->handle);
|
||||
$this->multi->handle = (new self())->multi->handle;
|
||||
$this->multi->handle = (new self([], $this->logger))->multi->handle;
|
||||
}
|
||||
|
||||
foreach ($options['resolve'] as $host => $ip) {
|
||||
|
@ -11,6 +11,8 @@
|
||||
|
||||
namespace Symfony\Component\HttpClient;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
||||
|
||||
/**
|
||||
@ -28,12 +30,20 @@ final class HttpClient
|
||||
*
|
||||
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options
|
||||
*/
|
||||
public static function create(array $defaultOptions = [], int $maxHostConnections = 6): HttpClientInterface
|
||||
public static function create(array $defaultOptions = [], LoggerInterface $logger = null, int $maxHostConnections = 6): HttpClientInterface
|
||||
{
|
||||
if (\extension_loaded('curl')) {
|
||||
return new CurlHttpClient($defaultOptions, $maxHostConnections);
|
||||
if (null === $logger) {
|
||||
$logger = new NullLogger();
|
||||
}
|
||||
|
||||
return new NativeHttpClient($defaultOptions, $maxHostConnections);
|
||||
if (\extension_loaded('curl')) {
|
||||
$logger->debug('Curl extension is enabled. Creating client.', ['client' => CurlHttpClient::class]);
|
||||
|
||||
return new CurlHttpClient($defaultOptions, $logger, $maxHostConnections);
|
||||
}
|
||||
|
||||
$logger->debug('Curl extension is disabled. Creating client.', ['client' => NativeHttpClient::class]);
|
||||
|
||||
return new NativeHttpClient($defaultOptions, $logger, $maxHostConnections);
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,8 @@
|
||||
|
||||
namespace Symfony\Component\HttpClient;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
use Symfony\Component\HttpClient\Exception\TransportException;
|
||||
use Symfony\Component\HttpClient\Response\NativeResponse;
|
||||
use Symfony\Component\HttpClient\Response\ResponseStream;
|
||||
@ -34,6 +36,7 @@ final class NativeHttpClient implements HttpClientInterface
|
||||
|
||||
private $defaultOptions = self::OPTIONS_DEFAULTS;
|
||||
private $multi;
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* @param array $defaultOptions Default requests' options
|
||||
@ -41,8 +44,10 @@ final class NativeHttpClient implements HttpClientInterface
|
||||
*
|
||||
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options
|
||||
*/
|
||||
public function __construct(array $defaultOptions = [], int $maxHostConnections = 6)
|
||||
public function __construct(array $defaultOptions = [], LoggerInterface $logger = null, int $maxHostConnections = 6)
|
||||
{
|
||||
$this->logger = $logger ?? new NullLogger();
|
||||
|
||||
if ($defaultOptions) {
|
||||
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, self::OPTIONS_DEFAULTS);
|
||||
}
|
||||
@ -68,6 +73,7 @@ final class NativeHttpClient implements HttpClientInterface
|
||||
*/
|
||||
public function request(string $method, string $url, array $options = []): ResponseInterface
|
||||
{
|
||||
$this->logger->notice('Making a request', ['url' => $url, 'method' => $method, 'client' => static::class]);
|
||||
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);
|
||||
|
||||
if ($options['bindto'] && file_exists($options['bindto'])) {
|
||||
|
@ -21,7 +21,8 @@
|
||||
"require": {
|
||||
"php": "^7.1.3",
|
||||
"symfony/contracts": "^1.1",
|
||||
"symfony/polyfill-php73": "^1.11"
|
||||
"symfony/polyfill-php73": "^1.11",
|
||||
"psr/log": "~1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"nyholm/psr7": "^1.0",
|
||||
|
Reference in New Issue
Block a user