Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fc6ba7e

Browse filesBrowse files
antonch1989nicolas-grekas
authored andcommitted
[HttpClient] logger integration
1 parent 4dfb741 commit fc6ba7e
Copy full SHA for fc6ba7e

File tree

4 files changed

+31
-7
lines changed
Filter options

4 files changed

+31
-7
lines changed

‎src/Symfony/Component/HttpClient/CurlHttpClient.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/CurlHttpClient.php
+9-2Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\HttpClient;
1313

14+
use Psr\Log\LoggerInterface;
15+
use Psr\Log\NullLogger;
1416
use Symfony\Component\HttpClient\Exception\TransportException;
1517
use Symfony\Component\HttpClient\Response\CurlResponse;
1618
use Symfony\Component\HttpClient\Response\ResponseStream;
@@ -34,15 +36,18 @@ final class CurlHttpClient implements HttpClientInterface
3436

3537
private $defaultOptions = self::OPTIONS_DEFAULTS;
3638
private $multi;
39+
private $logger;
3740

3841
/**
3942
* @param array $defaultOptions Default requests' options
4043
* @param int $maxHostConnections The maximum number of connections to a single host
4144
*
4245
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options
4346
*/
44-
public function __construct(array $defaultOptions = [], int $maxHostConnections = 6)
47+
public function __construct(array $defaultOptions = [], LoggerInterface $logger = null, int $maxHostConnections = 6)
4548
{
49+
$this->logger = $logger ?? new NullLogger();
50+
4651
if ($defaultOptions) {
4752
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, self::OPTIONS_DEFAULTS);
4853
}
@@ -86,6 +91,7 @@ public function __construct(array $defaultOptions = [], int $maxHostConnections
8691
*/
8792
public function request(string $method, string $url, array $options = []): ResponseInterface
8893
{
94+
$this->logger->notice('Making a request', ['url' => $url, 'method' => $method, 'client' => static::class]);
8995
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);
9096
$scheme = $url['scheme'];
9197
$authority = $url['authority'];
@@ -103,6 +109,7 @@ public function request(string $method, string $url, array $options = []): Respo
103109
];
104110

105111
if ('GET' === $method && !$options['body'] && $expectedHeaders === $pushedHeaders) {
112+
$this->logger->debug('Creating pushed response');
106113
// Reinitialize the pushed response with request's options
107114
$pushedResponse->__construct($this->multi, $url, $options);
108115

@@ -156,7 +163,7 @@ public function request(string $method, string $url, array $options = []): Respo
156163
// DNS cache removals require curl 7.42 or higher
157164
// On lower versions, we have to create a new multi handle
158165
curl_multi_close($this->multi->handle);
159-
$this->multi->handle = (new self())->multi->handle;
166+
$this->multi->handle = (new self([], $this->logger))->multi->handle;
160167
}
161168

162169
foreach ($options['resolve'] as $host => $ip) {

‎src/Symfony/Component/HttpClient/HttpClient.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/HttpClient.php
+13-3Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\HttpClient;
1313

14+
use Psr\Log\LoggerInterface;
15+
use Psr\Log\NullLogger;
1416
use Symfony\Contracts\HttpClient\HttpClientInterface;
1517

1618
/**
@@ -28,12 +30,20 @@ final class HttpClient
2830
*
2931
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options
3032
*/
31-
public static function create(array $defaultOptions = [], int $maxHostConnections = 6): HttpClientInterface
33+
public static function create(array $defaultOptions = [], LoggerInterface $logger = null, int $maxHostConnections = 6): HttpClientInterface
3234
{
35+
if (null === $logger) {
36+
$logger = new NullLogger();
37+
}
38+
3339
if (\extension_loaded('curl')) {
34-
return new CurlHttpClient($defaultOptions, $maxHostConnections);
40+
$logger->debug('Curl extension is enabled. Creating client.', ['client' => CurlHttpClient::class]);
41+
42+
return new CurlHttpClient($defaultOptions, $logger, $maxHostConnections);
3543
}
3644

37-
return new NativeHttpClient($defaultOptions, $maxHostConnections);
45+
$logger->debug('Curl extension is disabled. Creating client.', ['client' => NativeHttpClient::class]);
46+
47+
return new NativeHttpClient($defaultOptions, $logger, $maxHostConnections);
3848
}
3949
}

‎src/Symfony/Component/HttpClient/NativeHttpClient.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/NativeHttpClient.php
+7-1Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Component\HttpClient;
1313

14+
use Psr\Log\LoggerInterface;
15+
use Psr\Log\NullLogger;
1416
use Symfony\Component\HttpClient\Exception\TransportException;
1517
use Symfony\Component\HttpClient\Response\NativeResponse;
1618
use Symfony\Component\HttpClient\Response\ResponseStream;
@@ -34,15 +36,18 @@ final class NativeHttpClient implements HttpClientInterface
3436

3537
private $defaultOptions = self::OPTIONS_DEFAULTS;
3638
private $multi;
39+
private $logger;
3740

3841
/**
3942
* @param array $defaultOptions Default requests' options
4043
* @param int $maxHostConnections The maximum number of connections to open
4144
*
4245
* @see HttpClientInterface::OPTIONS_DEFAULTS for available options
4346
*/
44-
public function __construct(array $defaultOptions = [], int $maxHostConnections = 6)
47+
public function __construct(array $defaultOptions = [], LoggerInterface $logger = null, int $maxHostConnections = 6)
4548
{
49+
$this->logger = $logger ?? new NullLogger();
50+
4651
if ($defaultOptions) {
4752
[, $this->defaultOptions] = self::prepareRequest(null, null, $defaultOptions, self::OPTIONS_DEFAULTS);
4853
}
@@ -68,6 +73,7 @@ public function __construct(array $defaultOptions = [], int $maxHostConnections
6873
*/
6974
public function request(string $method, string $url, array $options = []): ResponseInterface
7075
{
76+
$this->logger->notice('Making a request', ['url' => $url, 'method' => $method, 'client' => static::class]);
7177
[$url, $options] = self::prepareRequest($method, $url, $options, $this->defaultOptions);
7278

7379
if ($options['bindto'] && file_exists($options['bindto'])) {

‎src/Symfony/Component/HttpClient/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/composer.json
+2-1Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
"require": {
2222
"php": "^7.1.3",
2323
"symfony/contracts": "^1.1",
24-
"symfony/polyfill-php73": "^1.11"
24+
"symfony/polyfill-php73": "^1.11",
25+
"psr/log": "~1.0"
2526
},
2627
"require-dev": {
2728
"nyholm/psr7": "^1.0",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.