fix(logging): replace print statements with structured logging#21
fix(logging): replace print statements with structured logging#21arunsanna wants to merge 3 commits intoGenAI-Security-Project:mainfrom
Conversation
Resolves GenAI-Security-Project#16 - Add module-level logger using logging.getLogger(__name__) - Replace 98 print() calls with appropriate log levels: - DEBUG: verbose details, extraction progress, field values - INFO: milestones, success messages, module load confirmation - WARNING: non-critical issues, missing optional data - ERROR: failures, exceptions, critical issues - Use lazy string formatting (logger.debug("val=%s", val)) instead of f-strings - Remove CRASH_DEBUG print statements (debugging artifacts) - Comment out auto-running test_registry_integration() on module import - Net reduction: 208 deletions, 139 insertions (cleaner code) Logging can be configured by consumers: import logging logging.basicConfig(level=logging.DEBUG) # See all output logging.basicConfig(level=logging.WARNING) # Quiet mode
There was a problem hiding this comment.
Pull request overview
This PR replaces print statements with Python's logging module across the AIBOM generator codebase. The primary goal is to introduce structured logging with appropriate log levels (DEBUG, INFO, WARNING, ERROR) instead of using print() statements.
Changes:
- Replaced 98 print() statements with logger method calls using lazy formatting
- Added module-level logger initialization using
logging.getLogger(__name__) - Cleaned up debugging artifacts (CRASH_DEBUG comments and print statements)
- Commented out auto-executing
test_registry_integration()to prevent it from running on module import
Comments suppressed due to low confidence (3)
HF_files/aibom-generator/src/aibom-generator/generator.py:803
- This logging call uses an f-string instead of lazy formatting. According to the PR's stated improvement of using lazy formatting (logger.debug("val=%s", val)), this should be changed to: logger.warning("Failed to fetch after %d attempts: %s", max_retries, e)
logger.warning(f"Failed to fetch after {max_retries} attempts: {e}")
HF_files/aibom-generator/src/aibom-generator/generator.py:805
- The
timemodule is used but never imported. This will cause a NameError at runtime when_fetch_with_retryis called and needs to retry.
time.sleep(1 * (attempt + 1)) # Exponential backoff
HF_files/aibom-generator/src/aibom-generator/generator.py:794
- Trailing space before closing parenthesis should be removed for consistency.
return license_urls.get(license_id.lower(), "https://spdx.org/licenses/" )
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Test Results ✅Ran full functional test with logging enabled: Output (sample): Results: Verified:
|
- Add missing `import time` for retry backoff (fixes NameError) - Convert remaining f-string to lazy formatting in logger.warning() - Remove trailing space in license_urls.get() return statement
The method was called but never defined, causing AttributeError. Replaced with a simple warning log for the edge case of no components in AIBOM.
Additional Copilot Review Feedback Addressed ✅Fixed undefined method bug:
This would have caused |
✅ Testing Completed - VERIFIEDTest Space: https://megamind1-aibom-pr21-logging.hf.space Test Results
Verificationgrep -c "print(" generator.py
# Result: 0Ready for merge. ✓ |
Status Update: Superseded by v0.2The structured logging implementation has been incorporated into the v0.2 branch architecture. Evidence: All modules now use: import logging
logger = logging.getLogger(__name__)Files using proper logging: This PR can be closed as structured logging is already in v0.2. |
Summary
print()statements with proper Pythonloggingmodule callslogger = logging.getLogger(__name__)Resolves #16
Changes
Key Improvements
logger.debug("val=%s", val)instead of f-strings (better perf)test_registry_integration()that ran on every importUsage
Consumers can configure logging level:
Test plan