Auto-inject Logger into LoggerAwareInterface services#23203
Open
Auto-inject Logger into LoggerAwareInterface services#23203
Conversation
Adds a Logger_Aware_Pass compiler pass that automatically calls setLogger on every registered service whose class implements Psr\Log\LoggerAwareInterface. Developers can now opt into logging by implementing the interface and using LoggerAwareTrait, instead of threading the Logger through the constructor. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Coverage Report for CI Build 24Warning Build has drifted: This PR's base is out of sync with its target branch, so coverage data may include unrelated changes. Coverage decreased (-0.6%) to 52.831%Details
Uncovered ChangesNo uncovered changes found. Coverage RegressionsNo coverage regressions found. Coverage Stats💛 - Coveralls |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds DI-container plumbing to automatically inject the plugin’s Yoast\WP\SEO\Loggers\Logger into any service that opts into PSR-3 logger awareness via LoggerAwareInterface, avoiding constructor wiring.
Changes:
- Introduces a new Symfony DI compiler pass (
Logger_Aware_Pass) that adds asetLogger()method call to eligible service definitions. - Skips injection when a service definition already has an explicit
setLoggermethod call configured. - Registers the new compiler pass in
Container_Compiler::compile()afterInject_From_Registry_Pass.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| config/dependency-injection/logger-aware-pass.php | Adds the new compiler pass that detects LoggerAwareInterface implementations and injects the Logger via setLogger(). |
| config/dependency-injection/container-compiler.php | Registers Logger_Aware_Pass in the container compilation pipeline. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Update docblock to reference the prefixed YoastSEO_Vendor namespace, matching what the pass actually checks. - Normalize the manual setLogger method-call check with strtolower so case-variant configurations (e.g. setlogger) still suppress auto-injection, since PHP method names are case-insensitive.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Context
We want to encourage richer logging across the codebase, but threading the
Loggerservice through every constructor (and updating every test double) is a lot of friction for what is usually an opt-in concern. PSR-3 already standardizes this withPsr\Log\LoggerAwareInterface+LoggerAwareTrait, so this PR teaches our DI container to wire it up automatically.By default the injected logger is our own
Yoast\WP\SEO\Loggers\Logger, which wraps aNullLoggerunless a consumer overrides it through thewpseo_loggerfilter — so opting in costs nothing at runtime until someone actually plugs in a real logger.We don't use this interface anywhere yet, with one exception: the open PR for the MyYoast OAuth client uses it. Since logger-awareness is part of PSR-3 and is the de facto standard, this seems like a good convention to adopt going forward.
Summary
This PR can be summarized in the following changelog entry:
Logger_Aware_Passcompiler pass so any service implementingPsr\Log\LoggerAwareInterfaceautomatically receives theLoggerviasetLogger(), with no constructor wiring required.Relevant technical choices
config/dependency-injection/logger-aware-pass.php) in line with the existingLoader_Pass,Interface_Injection_PassandInject_From_Registry_Pass. Registered inContainer_Compiler::compile()afterInject_From_Registry_Pass.$container->getDefinitions(), skips abstract definitions, and callsaddMethodCall( 'setLogger', [ new Reference( Logger::class ) ] )on every definition whose class implementsLoggerAwareInterface.setLoggermethod call configured elsewhere, the pass leaves it alone (continue 2). This keeps manual overrides inservices.phpauthoritative.LoggerAwareInterfaceis referenced via the prefixedYoastSEO_Vendor\Psr\Log\LoggerAwareInterfacebecause PSR-Log is scoped at build time, while the Symfony DI imports stay un-prefixed (the dump rewrite inContainer_Compilerswaps those at dump time).implements LoggerAwareInterface+use LoggerAwareTrait;— that's the entire change required at the call site.Test instructions
Test instructions for the acceptance test before the PR gets merged
This PR has no user-facing impact on its own — it only adds plumbing that takes effect when a service opts in. To acceptance-test that the plumbing is wired up correctly:
composer install.composer compile-dito rebuildsrc/generated/container.php.composer test(PHP unit tests) and confirm everything still passes.use LoggerAwareTraitfor quick implementation).composer compile-di.setLoggeris called insrc/generated/container.phpwith the instance of our existing Logger class.Relevant test scenarios
This is an infrastructure-only change with no UI surface, so the matrix above isn't really applicable. A smoke test on the latest WordPress + a current PHP (7.4 / 8.x) is sufficient.
Test instructions for QA when the code is in the RC
QA can test this PR by activating Yoast SEO on a clean WordPress install and confirming there is no functional change versus the previous release.
Impact check
This PR affects the following parts of the plugin, which may require extra testing:
src/generated/container.php). Every service in the container is examined by the new pass at compile time. At runtime, only services implementingLoggerAwareInterfaceare affected — currently none in this repository — so the runtime blast radius is effectively zero.Other environments
[shopify-seo], added test instructions for Shopify and attached theShopifylabel to this PR.[yoast-doc-extension], added test instructions for Yoast SEO for Google Docs and attached theGoogle Docs Add-onlabel to this PR.Documentation
Quality assurance
grunt build:imagesand commited the results, if my PR introduces new images or SVGs.Innovation
innovationlabel.