speechbrain.utils.logger moduleο
Managing the logger, utilities
- Author
Fang-Pen Lin 2012 https://fangpenlin.com/posts/2012/08/26/good-logging-practice-in-python/
Peter Plantinga 2020
Aku Rouhe 2020
Summaryο
Classes:
Logger adapter that handles multi-process logging, ensuring logs are written only on the main process if specified. |
|
TQDM compatible StreamHandler. |
Functions:
Formats number to the appropriate order of magnitude for printing. |
|
Returns a string describing the current Python / SpeechBrain environment. |
|
Retrieves a logger with the specified name, applying a log level from the environment variable |
|
Setup logging configuration. |
Referenceο
- class speechbrain.utils.logger.MultiProcessLoggerAdapter(logger, extra=None)[source]ο
Bases:
LoggerAdapterLogger adapter that handles multi-process logging, ensuring logs are written only on the main process if specified. This class extends
logging.LoggerAdapterand provides additional functionality for controlling logging in multi-process environments, with the option to limit logs to the main process only.This class is heavily inspired by HuggingFace Accelerate toolkit: https://github.com/huggingface/accelerate/blob/85b1a03552cf8d58e036634e004220c189bfb247/src/accelerate/logging.py#L22
- log(level: int, msg: str, *args, **kwargs)[source]ο
Logs a message with the specified log level, respecting the
main_process_onlyflag to decide whether to log based on the current process.- Parameters:
level (int) β Logging level (e.g., logging.INFO, logging.WARNING).
msg (str) β The message to log.
*args (tuple) β Additional positional arguments passed to the logger.
**kwargs (dict) β Additional keyword arguments passed to the logger, including: - main_process_only (bool): If True, log only from the main process (default: True). - stacklevel (int): The stack level to use when logging (default: 2).
Notes
If
main_process_onlyis True, the log will only be written if the current process is the main process, as determined byif_main_process().
- warning_once(*args, **kwargs)[source]ο
Logs a warning message only once by using caching to prevent duplicate warnings.
- Parameters:
Notes
This method is decorated with
functools.lru_cache(None), ensuring that the warning message is logged only once regardless of how many times the method is called.
- speechbrain.utils.logger.get_logger(name: str) MultiProcessLoggerAdapter[source]ο
Retrieves a logger with the specified name, applying a log level from the environment variable
SB_LOG_LEVELif set, or defaults toINFOlevel.If the environment variable
SB_LOG_LEVELis not defined, it defaults toINFOlevel and sets this level in the environment for future use. The environment variable can be set manually or automatically inBrainclass followingsetup_logging.- Parameters:
name (str) β The name of the logger to retrieve.
- Returns:
An instance of
MultiProcessLoggerAdapterwrapping the logger with the specified name.- Return type:
- speechbrain.utils.logger.setup_logging(config_path='log-config.yaml', overrides={}, default_level='DEBUG')[source]ο
Setup logging configuration.
- Parameters:
config_path (str) β The path to a logging config file.
overrides (dict) β A dictionary of the same structure as the config dict with any updated values that need to be applied.
default_level (str) β The log level to use if the config file is not found. Python logging allows ints or strings: https://docs.python.org/3/library/logging.html#logging.Logger.setLevel but strings are used here as environment variables have to be strings. The available levels are listed here: https://docs.python.org/3/library/logging.html#levels
- class speechbrain.utils.logger.TqdmCompatibleStreamHandler(stream=None)[source]ο
Bases:
StreamHandlerTQDM compatible StreamHandler.
Writes and prints should be passed through tqdm.tqdm.write so that the tqdm progressbar doesnβt get messed up.
- speechbrain.utils.logger.format_order_of_magnitude(number, abbreviate=True)[source]ο
Formats number to the appropriate order of magnitude for printing.
- Parameters:
- Returns:
The formatted number. Note that the order of magnitude token is part of the string.
- Return type:
Example
>>> print(format_order_of_magnitude(123456)) 123.5k >>> print(format_order_of_magnitude(0.00000123, abbreviate=False)) 1.2 millionths >>> print(format_order_of_magnitude(5, abbreviate=False)) 5
- speechbrain.utils.logger.get_environment_description()[source]ο
Returns a string describing the current Python / SpeechBrain environment.
Useful for making experiments as replicable as possible.
- Returns:
The string is formatted ready to be written to a file.
- Return type:
Example
>>> get_environment_description().splitlines()[0] 'SpeechBrain system description'