You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a draft PR for early feedback from maintainers before finalizing implementation.
Summary
This PR introduces a centralized logging system and a structured exception wrapper to replace scattered print() usage and raw exceptions across the codebase.
The goal is to:
Improve observability and debugging
Provide consistent logging across modules
Surface actionable error messages with precise file and line information
The implementation relies only on Python’s standard library.
Global log level control: set_level(logging.DEBUG)
Child loggers inherit configuration automatically
Safe initialization (no duplicate handlers)
2. Structured Exception Wrapper (exception.py)
Adds PerceptionMetricsException
Wraps original exceptions and extracts:
File name
Line number (from the last traceback frame)
Standardized error format: Error in [file.py] at line [42]: original error message
Example Usage
Logging
from perceptionmetrics.utils.logging_config import get_logger
_logger = get_logger(__name__)
_logger.info("Loaded %d samples", n)
Enable File Logging (Optional)
from perceptionmetrics.utils.logging_config import add_file_handler
add_file_handler("logs/run.log")
Exception Handling
from perceptionmetrics.utils.exception import PerceptionMetricsException
try:
model = torch.jit.load(path)
except Exception as e:
raise PerceptionMetricsException(e) from e
Changes Made
Added perceptionmetrics/utils/logging_config.py
Added perceptionmetrics/utils/exception.py
Updated models/__init__.py to use centralized logging and structured exceptions.
Replaced print() statements with logger usage in modified modules.
Testing
Verified console logging output across modules.
Verified file logging via add_file_handler("logs/run.log")
Tested exception wrapping with real errors to confirm file/line accuracy.
Hi @dpascualhe, opening this as a draft PR as requested.
The core get_logger() helper and one example usage in models/__init__.py are in place. Before applying this pattern across the rest of the codebase, I’d like to confirm a few key design decisions:
File logging — exposure
Currently, file logging is opt-in via add_file_handler("logs/run.log").
Would you prefer this to remain a programmatic API, or be exposed via CLI flags (e.g., --log-file)?
Default log level
Currently set to INFO, which may be noisy when the library is imported externally.
Would WARNING be a better default?
CLI output vs logging (cli/batch.py)
Some print() calls are used for clean, user-facing summaries.
Should these remain as print(), or be routed through the logger for consistency?
Scope of this PR (Streamlit / GUI scope )
Should logging changes be limited to the core library + CLI, or also include Streamlit/UI files?
Happy to proceed with the full migration across the codebase once I have your guidance.
Thanks!
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
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.
Related to #516
Status
This is a draft PR for early feedback from maintainers before finalizing implementation.
Summary
This PR introduces a centralized logging system and a structured exception wrapper to replace scattered
print()usage and raw exceptions across the codebase.The goal is to:
The implementation relies only on Python’s standard library.
Key Features
1. Centralized Logging (
logging_config.py)"perceptionmetrics"add_file_handler("logs/run.log")set_level(logging.DEBUG)2. Structured Exception Wrapper (
exception.py)PerceptionMetricsExceptionError in [file.py] at line [42]: original error messageExample Usage
Logging
from perceptionmetrics.utils.logging_config import get_logger_logger = get_logger(__name__)_logger.info("Loaded %d samples", n)Enable File Logging (Optional)
from perceptionmetrics.utils.logging_config import add_file_handleradd_file_handler("logs/run.log")Exception Handling
Changes Made
perceptionmetrics/utils/logging_config.pyperceptionmetrics/utils/exception.pymodels/__init__.pyto use centralized logging and structured exceptions.print()statements with logger usage in modified modules.Testing
add_file_handler("logs/run.log")