env
Environment Utility API
This module provides utility functions for managing the environment
Classes:
-
MarkdownRichHandler
–Custom logging handler for Rich library with Markdown support.
Functions:
-
get_version
–Get the version of the package
-
setup_logger
–Set up a logger with optional file output
-
suppress_os_stdio
–Context manager to suppress OS stdout and stderr
Copyright 2025 Ambiq. All Rights Reserved.
Functions
get_version
setup_logger
setup_logger(log_name: str, level: int | None = None, recursive: bool = True, file_path: str | Path | None = None) -> logging.Logger
Configure a logger with optional file output.
This function ensures that the logger is only initialized once per name. The logger can be configured to propagate messages to the root logger.
Included handlers are
- RichHandler: For console output with rich formatting.
- FileHandler: For file output (optional).
Parameters:
-
log_name
str
) –Name of the logger.
-
level
int
, default:None
) –Logging level (0: ERROR, 1: INFO, 2: DEBUG). Defaults to None.
-
recursive
bool
, default:True
) –If True, use the root logger name. Defaults to True.
-
file_path
str | Path
, default:None
) –Path to the log file. Defaults to None.
Returns:
-
Logger
–logging.Logger: Configured logger instance.
suppress_os_stdio
Redirects the OS stdout (fd=1) and stderr (fd=2) to /dev/null, then restores them on exit.
This is useful for suppressing output from subprocesses or external libraries that write to stdout/stderr. This context manager will flush the Python buffers before redirecting and restore the original file descriptors after exiting the context.
Example usage: