-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[FrameworkBundle] revert implementing LoggerAwareInterface in HTTP client decorators #54674
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
xabbuh
commented
Apr 19, 2024
Q | A |
---|---|
Branch? | 7.1 |
Bug fix? | no |
New feature? | no |
Deprecations? | no |
Issues | |
License | MIT |
@@ -28,7 +28,7 @@ | |||
* | |||
* @author Jérémy Derussé <[email protected]> | |||
*/ | |||
class RetryableHttpClient implements HttpClientInterface, LoggerAwareInterface, ResetInterface | |||
class RetryableHttpClient implements HttpClientInterface, ResetInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implementing the LoggerAwareInterface
when the constructor already receives a logger doesn't look reasonable to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also wonder if we shouldn't change the other decorators to let them optionally receive a logger in their constructor too instead of implementing the LoggerAwareInterface
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doing so would also allow us to revert the composer.json
changes
bbc39d2
to
3d39484
Compare
@@ -46,6 +46,10 @@ public function __construct(HttpClientInterface $client, ?RetryStrategyInterface | |||
$this->strategy = $strategy ?? new GenericRetryStrategy(); | |||
$this->maxRetries = $maxRetries; | |||
$this->logger = $logger; | |||
|
|||
if (null !== $logger && $this->client instanceof LoggerAwareInterface) { | |||
$this->client->setLogger($logger); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not convinced that a decorator should actually modify the decorated HTTP client this way. IMO setting the logger is the user's responsibility when constructing the client.
Looking at your comments here, I wonder if we should keep #54668 or not? |
Sure, On that project, constructing an final class HttpClientFactory
{
public function __construct(
private readonly HttpClientInterface $http,
) {
}
public function create(Config $config, LoggerInterface $logger): HttpClientInterface
{
$client = match (...) {
Config::A => $this->clientA(),
Config::B => $this->clientB(),
};
if ($client instanceof LoggerAwareInterface) {
$client->setLogger($logger);
}
return $client;
}
private function clientA(): HttpClientInterface
{
return $this->http->withOptions([
...
]);
}
private function clientB(): HttpClientInterface
{
return $this->http->withOptions([
...
]);
}
} I'm using As you can see, the #[WithMonologChannel('feature1')]
final class Feature1
{
public function __construct(
private readonly HttpClientFactory $httpClientFactory,
private readonly LoggerInterface $logger,
) {
}
public function method(Config $config): void
{
$http = $this->httpClientFactory->create($config, $this->logger);
...
}
}
#[WithMonologChannel('feature2')]
final class Feature2
{
public function __construct(
private readonly HttpClientFactory $httpClientFactory,
private readonly LoggerInterface $logger,
) {
}
public function method(Config $config): void
{
$http = $this->httpClientFactory->create($config, $this->logger);
...
}
} I've only proposed adding |
This comment was marked as outdated.
This comment was marked as outdated.
setLogger()
calls for registered HTTP clients
This comment was marked as outdated.
This comment was marked as outdated.
What about supporting passing the logger to private function clientA(LoggerInterface $logger): HttpClientInterface
{
return $this->http->withOptions([
'extra' => [
'logger' => $logger,
],
// ...
]);
} |
I wonder if we need this PR at all: to me, the actual http client that does log should already have the logger wired. Did you check this aspect? |
8ae7a05
to
436bf31
Compare
setLogger()
calls for registered HTTP clients
I agree with this reasoning. That's why I suggest to revert implementing the |
I don't have any opinion on this honestly |
The actual clients do use the logger themselves. They do not use this property to configure another client. We can argue if using the interface was the right decision back when we introduced it, but that’s a different topic. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
We could even deprecate existing setLogger proxies.
Thank you @xabbuh. |
see #54806 |
…ing clients (xabbuh) This PR was merged into the 7.1 branch. Discussion ---------- [HttpClient] deprecate setLogger() methods of decorating clients | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | no | New feature? | no | Deprecations? | yes | Issues | Fix #54674 (review) | License | MIT Commits ------- 9d95152 deprecate setLogger() methods of decorating clients