Skip to content

Commit

Permalink
minor #54668 [HttpClient] Implemented LoggerAwareInterface in HttpCli…
Browse files Browse the repository at this point in the history
…entInterface decorators (yann-eugone)

This PR was squashed before being merged into the 7.1 branch.

Discussion
----------

[HttpClient] Implemented LoggerAwareInterface in HttpClientInterface decorators

Implement `Psr\Log\LoggerAwareInterface` in `Symfony\Component\HttpClient\UriTemplateHttpClient`.

| Q             | A
| ------------- | ---
| Branch?       | 7.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Issues        |
| License       | MIT

I was on a journey to enhance our logs on a project that highly depend on `HttpClient`.
I started to separate logs per feature, and I wanted to include `HttpClient` logs to the logger of the currently running the feature.

I figured out that most implementations of `HttpClientInterface` are implementing `LoggerAwareInterface`, so I decided to just call `$client->setLogger(...)` on my side.
And **nothing changed**.

Nothing changed because my logger is decorated by the `UriTemplateHttpClient` that is not implementing `LoggerAwareInterface`.
Althought `UriTemplateHttpClient` has no need of a logger, as a decorator it should be able to act like it, so the underlying client can be reached.
This is what is done for instance in `Symfony\Component\HttpClient\TraceableHttpClient`.

Commits
-------

c2a7b73 [HttpClient] Implemented LoggerAwareInterface in HttpClientInterface decorators
  • Loading branch information
nicolas-grekas committed Apr 19, 2024
2 parents 13ab9eb + c2a7b73 commit 924e4d8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/Symfony/Component/HttpClient/CachingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\HttpClient;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Response\MockResponse;
use Symfony\Component\HttpClient\Response\ResponseStream;
use Symfony\Component\HttpFoundation\Request;
Expand All @@ -31,7 +33,7 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
class CachingHttpClient implements HttpClientInterface, ResetInterface
class CachingHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
{
use HttpClientTrait;

Expand Down Expand Up @@ -142,4 +144,11 @@ public function reset(): void
$this->client->reset();
}
}

public function setLogger(LoggerInterface $logger): void
{
if ($this->client instanceof LoggerAwareInterface) {
$this->client->setLogger($logger);
}
}
}
11 changes: 10 additions & 1 deletion src/Symfony/Component/HttpClient/EventSourceHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\HttpClient;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Chunk\ServerSentEvent;
use Symfony\Component\HttpClient\Exception\EventSourceException;
use Symfony\Component\HttpClient\Response\AsyncContext;
Expand All @@ -25,7 +27,7 @@
* @author Antoine Bluchet <soyuka@gmail.com>
* @author Nicolas Grekas <p@tchwork.com>
*/
final class EventSourceHttpClient implements HttpClientInterface, ResetInterface
final class EventSourceHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
{
use AsyncDecoratorTrait, HttpClientTrait {
AsyncDecoratorTrait::withOptions insteadof HttpClientTrait;
Expand Down Expand Up @@ -156,4 +158,11 @@ public function request(string $method, string $url, array $options = []): Respo
}
});
}

public function setLogger(LoggerInterface $logger): void
{
if ($this->client instanceof LoggerAwareInterface) {
$this->client->setLogger($logger);
}
}
}
11 changes: 10 additions & 1 deletion src/Symfony/Component/HttpClient/RetryableHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Symfony\Component\HttpClient;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpClient\Response\AsyncContext;
use Symfony\Component\HttpClient\Response\AsyncResponse;
Expand All @@ -27,7 +28,7 @@
*
* @author Jérémy Derussé <jeremy@derusse.com>
*/
class RetryableHttpClient implements HttpClientInterface, ResetInterface
class RetryableHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
{
use AsyncDecoratorTrait;

Expand Down Expand Up @@ -163,6 +164,14 @@ public function request(string $method, string $url, array $options = []): Respo
});
}

public function setLogger(LoggerInterface $logger): void
{
$this->logger = $logger;
if ($this->client instanceof LoggerAwareInterface) {
$this->client->setLogger($logger);
}
}

private function getDelayFromHeader(array $headers): ?int
{
if (null !== $after = $headers['retry-after'][0] ?? null) {
Expand Down
11 changes: 10 additions & 1 deletion src/Symfony/Component/HttpClient/ThrottlingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\HttpClient;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\RateLimiter\LimiterInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
Expand All @@ -19,7 +21,7 @@
/**
* Limits the number of requests within a certain period.
*/
class ThrottlingHttpClient implements HttpClientInterface, ResetInterface
class ThrottlingHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
{
use DecoratorTrait {
reset as private traitReset;
Expand Down Expand Up @@ -48,4 +50,11 @@ public function reset(): void
$this->traitReset();
$this->rateLimiter->reset();
}

public function setLogger(LoggerInterface $logger): void
{
if ($this->client instanceof LoggerAwareInterface) {
$this->client->setLogger($logger);
}
}
}
11 changes: 10 additions & 1 deletion src/Symfony/Component/HttpClient/UriTemplateHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

namespace Symfony\Component\HttpClient;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Symfony\Contracts\Service\ResetInterface;

class UriTemplateHttpClient implements HttpClientInterface, ResetInterface
class UriTemplateHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface
{
use DecoratorTrait;

Expand Down Expand Up @@ -62,6 +64,13 @@ public function withOptions(array $options): static
return $clone;
}

public function setLogger(LoggerInterface $logger): void
{
if ($this->client instanceof LoggerAwareInterface) {
$this->client->setLogger($logger);
}
}

/**
* @return \Closure(string $url, array $vars): string
*/
Expand Down

0 comments on commit 924e4d8

Please sign in to comment.