refactor: enhance Config and command base with comprehensive scenario-aware logging#4488
Merged
cidrblock merged 13 commits intoansible:mainfrom Jul 28, 2025
Merged
Conversation
…and state.py - Remove unused LOG = logging.getLogger(__name__) definitions - Remove corresponding logging imports where no longer needed - Fix Callable import in state.py to use collections.abc
- Remove unused LOG = logging.getLogger(__name__) from command modules - Remove unused LOG definitions from dependency, driver, and provisioner modules - Remove corresponding logging imports where no longer needed - Clean up state.py, platforms.py, verifier/testinfra.py and others - All pre-commit checks passing after cleanup
- Remove unused LOG = logging.getLogger(__name__) from command/base.py - Keep logging import as it's still used in one place (line 137) - All LOG definitions are now used (verified 6 remaining files)
- Remove environment detection test cases (not part of unused logger cleanup) - Remove TTY detection test function (belongs in separate environment detection PR) - Keep focus on pure unused logger cleanup only
- Add step context to scenario discovery logging (discovery) - Add step context to prerun logging (prerun) - Add step context to reset scenario logging (reset) - Add step context to error cleanup logging (action/cleanup) - Add step context to default scenario fallback warning (subcommand)
- Replace LOG.warning with scenario logger in Config.state property - Add step context 'config' for configuration-related warnings - Import logger module for scenario context support
Add @Property _log method to Config class that provides scenario-aware logging with 'config' step context. Update existing logger call in state property to use self._log instead of creating ad-hoc logger. This enables other Config methods to use self._log for consistent scenario and step context in log messages.
Complete conversion of Config class logging to use scenario context: - _validate(): Use hardcoded scenario logger during validation - _get_driver_name(): Use self._log for driver warnings - collection(): Use self._log for galaxy.yml validation warnings Remove unused module-level logger (LOG) and import since all logging now uses scenario-aware loggers with proper step context. Benefits: - All Config logging now includes scenario and step information - Consistent logging pattern throughout Config class - Eliminates context-less logging in Config operations
654cf7b to
44c8d6e
Compare
…unction - Add global _log() function in base.py for on-demand logger creation - Replace 5 manual get_scenario_logger() calls with single reusable function - Simplify logger usage: _log(scenario_name, step, message, level='info') - Pre-format messages instead of using format placeholders - Cleaner, more maintainable logging pattern across command base
d2a65b1 to
bd2fb4e
Compare
- Replace patched_logger_debug mock with caplog for better testing approach - Verify debug message content includes molecule file path - Check scenario logger extras: molecule_scenario and molecule_step - Ensure validate step is correctly recorded in log record - More robust testing of scenario-aware logging functionality
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR enhances the Config class and command base with comprehensive scenario-aware logging by consolidating scattered logger calls into a unified pattern with full scenario and step context throughout all operations.
- Adds scenario-aware logging context to Config operations including validation, driver checks, and file modifications
- Replaces scattered
get_scenario_logger()calls with a single global_log()function in command base - Updates test suite to use
caplogand verify scenario logger extras
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/molecule/config.py | Adds _log property for scenario-aware logging and converts all Config logging calls to use contextual loggers |
| src/molecule/command/base.py | Introduces global _log() function to replace scattered logger initializations and standardize logging pattern |
| tests/unit/test_config.py | Updates test to use caplog instead of mocking and validates scenario logger extras |
cidrblock
added a commit
to cidrblock/molecule
that referenced
this pull request
Jul 28, 2025
…-properties - Merge PR ansible#4488 comprehensive work into PR ansible#4486 - Includes Config class scenario-aware logging - Includes global _log function for command base - Includes all test improvements and documentation
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.
Summary
Comprehensive enhancement of Config class and command base logging with scenario-aware context, building on the foundation from merged PRs #4483 and #4484.
User-Facing Changes
Enhanced logging context across all Config and command operations:
Before: Generic logging without context
After: Complete scenario and step context
Technical Implementation
🏗️ Config Class Enhancements:
@property _logmethod to Config class for dynamic scenario context🧹 Command Base Logger Consolidation:
get_scenario_logger()calls with single global_log()function_log(scenario_name, step, message, level='info')✅ Enhanced Test Coverage:
test_validateto usecaploginstead of mock patchingmolecule_scenarioandmolecule_stepChanges Included
Config Class (4 commits):
feat: add step context to command base logger callsfeat: add scenario context to config file modification warningrefactor: add reusable _log property to Config classrefactor: convert all Config logging to scenario-aware loggingCommand Base Improvements (2 commits):
5.
refactor: replace scattered logger initializations with global _log function6.
fix: update test_validate to use caplog and check scenario logger extrasDependencies (Included):
Dependencies
Testing
molecule syntaxconfirms enhanced logging worksImpact
_logfunction replaces scattered initializations