This manual is useful, however, it does not specify whether it's recommended to inject the logging channel through a public setter or through the constructor for your object. Currently in MediaWiki core this seems to be inconsistent, which makes it confusing. Here is to note how all core services defined here ( https://github.com/wikimedia/mediawiki/blob/master/includes/ServiceWiring.php ) currently do it:
\MimeAnalyzer
: Constructor\MediaWiki\Storage\NameTableStoreFactory
: Constructor\OldRevisionImporter
: Constructor\MediaWiki\Preferences\PreferencesFactory
: Setter\MediaWiki\Revision\RevisionRenderer
: Setter\MediaWiki\Revision\RevisionStoreFactory
: Constructor\MediaWiki\Shell\CommandFactory
: Setter\UploadRevisionImporter
: Constructor\ImportableOldRevisionImporter
: Constructor
From my knowledge, a service **must** be immutable. By providing the logger to the object through a setter means it's can be changed even after instantiation, which goes against practice of dependency injection. Plus, with the fact that it can still be changed, it means it's harder to debug which seems to be counter-intuitive. in the document, it mentions SHOULD: Use dependency injection
under the Architecture section. Personally, I prefer to use constructor injection in general.
I believe we should have a standard on this, and this document should be suggesting which way. What is the benefit of having a class implement the \Psr\Log\LoggingAwareInterface
and use the \Psr\Log\LoggerAwareTrait
to satisfy the interface instead? Shouldn't it always be done through the constructor? I feel like this is a discussion worth having; please let me know if I've misunderstood. thank you.