LoggerAwareInterface::setLogger() vs injecting LoggerInterface into the constructor #53921
Unanswered
webignition
asked this question in
Q&A
Replies: 1 comment
-
Note that the documentation also mention classic injection in its first section:
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In preparing to add some logging to a service, I decided to read the latest logging documentation to check that I wasn't heading in the wrong direction.
The documentation suggests implementing
LoggerAwareInterface::setLogger()
as a means of using a logger inside a service.I was expecting, as with other dependencies, to inject a
LoggerInterface
instance into the service constructor. I wasn't aware that implementingLoggerAwareInterface::setLogger()
is also supported.Implementing
LoggerAwareInterface::setLogger()
seems a little more verbose and allows the logger to be changed after the service has been instantiated which seems like a bit of unnecessary extra functionality. Injecting aLoggerInterface
instance seems more in line with how service dependencies are otherwise handled and therefore a little more consistent.Is there any advantage of implementing
LoggerAwareInterface::setLogger()
over injecting aLoggerInterface
instance into the constructor? Since the former is mentioned in the logging documentation, I can't help but wonder if there is a benefit to this approach that I'm failing to see.Beta Was this translation helpful? Give feedback.
All reactions