ahvn.utils.basic.log_utils module¶
- ahvn.utils.basic.log_utils.get_logger(name, level=None, fmt=None, datefmt=None, style='%', *, validate=True, colors=None)[source]¶
Get a logger with a custom colored formatter.
- Parameters:
name (str) – The name of the logger.
level (Optional[Union[str, int]]) – The default log level.
fmt (Optional[str]) – The log message format.
datefmt (Optional[str]) – The date format.
colors (Optional[dict[int, Callable[[Any, bool], str]]]) – Custom color functions for log levels.
style (str)
validate (bool)
- Returns:
The configured logger.
- Return type:
- ahvn.utils.basic.log_utils.set_log_level(level, loggers=None)[source]¶
Set the log level globally for ahvn and optionally other loggers.
- Parameters:
- Return type:
Example
>>> set_log_level("WARNING") # Suppress INFO/DEBUG messages >>> set_log_level(logging.DEBUG, ["ahvn", "myapp"]) # Enable debug for specific loggers
- ahvn.utils.basic.log_utils.redirect_logs(filepath, loggers=None, level=None, fmt=None)[source]¶
Redirect ahvn logging output to a file.
- Parameters:
filepath (str) – Path to the log file.
loggers (Optional[List[str]]) – List of logger names to redirect. If None, redirects “ahvn” logger.
level (Optional[Union[str, int]]) – Log level for the file handler. If None, uses the current logger level.
fmt (Optional[str]) – Log message format.
- Return type:
Example
>>> redirect_logs("/tmp/ahvn.log") >>> redirect_logs("/tmp/debug.log", level="DEBUG", loggers=["ahvn", "myapp"])
- ahvn.utils.basic.log_utils.restore_logs(loggers=None)[source]¶
Restore original log handlers for specified loggers.
- Parameters:
loggers (Optional[List[str]]) – List of logger names to restore. If None, restores all loggers that were modified.
- Return type:
Example
>>> restore_logs() # Restore all modified loggers >>> restore_logs(["ahvn"]) # Restore specific logger
- class ahvn.utils.basic.log_utils.SuppressOutput(suppress_stdout=True, suppress_stderr=True)[source]¶
Bases:
objectContext manager to suppress stdout/stderr at the OS file descriptor level.
This is more aggressive than redirecting sys.stdout/sys.stderr and can capture output from C libraries and subprocesses as well.
Use restore() and suppress() methods for temporary restoration during the suppression context (e.g., for updating progress displays).
Example
>>> with SuppressOutput(): ... # All output suppressed here ... noisy_library_call()
>>> with SuppressOutput(suppress_stderr=False): ... # Only stdout suppressed, stderr still visible ... pass
>>> with SuppressOutput() as suppressor: ... noisy_setup() ... suppressor.restore() # Temporarily show output ... print("Progress update") ... suppressor.suppress() # Continue suppressing ... more_noisy_code()