Skip to main content

logging_utils

Utilities for logging and warning messages functionality.

Module

Functions

deprecated_class_name

def deprecated_class_name(cls: Type[_T])> Callable[..., ~_T]:

Class decorator to log a DeprecationWarning for deprecated class names.

Used for cases where we have:

class

#### filter_stderr
<!-- prettier-ignore -->

```py showLineNumbers
str, 'codeBlockAnchor[re.Pattern

Filter stderr messages emitted within this context manager.

Will remove any messages where the start matches the filter pattern whilst allowing any other messages to go through.

Arguments

  • to_filter: Regex pattern to match the start of messages to be filtered.

is_not_pytorch_lightning_warning

def is_not_pytorch_lightning_warning(record: logging.LogRecord)> int:

Returns 0 if is a warning generated by PyTorch Lightning, 1 otherwise.

log_pytorch_env_info_if_available

def log_pytorch_env_info_if_available()> None:

Log PyTorch environment info if PyTorch is available.

setup_loggers

def setup_loggers(    loggers: list[logging.Logger],    log_file_dir_name: Optional[str] = None,    log_level: Union[int, str] = 20,    clear_existing_handlers: bool = True,    clear_existing_filters: bool = True,)> list:

Set up supplied loggers with stdout and file handlers.

Creates a logfile in 'logs' directory with the current date and time and outputs all logs at the "DEBUG" level. Also outputs logs to stdout at the "INFO" level. A common scenario is to attach handlers only to the root logger, and to let propagation take care of the rest.

Arguments

  • loggers: The logger(s) to set up
  • log_file_dir_name: Creates a subdirectory inside BITFOUNT_LOGS_DIR if provided. Defaults to None.
  • log_level: The log level to apply to the console logs
  • clear_existing_handlers: If True, clear existing handlers for each logger
  • clear_existing_filters: If True, clear existing filters for each logger

Returns A list of updated logger(s).

Classes

SampleFilter

class SampleFilter(interval: int = 100, *args: Any, **kwargs: Any):

A logging filter that tracks counts for logs and only logs at intervals.

Useful for very frequent logs where we only care about logging "samples" of the logs, at some specific interval, for example, whilst iterating through a list of files.

Initialize a filter.

Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.

Methods


filter

def filter(self, record: logging.LogRecord)> bool:

Returns True if the record should be logged, False otherwise.