nemo_automodel.components.loggers.log_utils

View as Markdown

Module Contents

Classes

NameDescription
ColorFormatterLogging formatter that colorizes the level name and includes date/time.
RankFilterA logging filter that controls log output based on the process rank.

Functions

NameDescription
_ensure_root_handler_with_formatterEnsure the root logger has at least one StreamHandler with the given formatter.
add_filter_to_all_loggersAdd a filter to the root logger and all existing loggers.
module_filterLogging filter to exclude messages from specific modules.
setup_loggingSet up logging level and filters for the application.

Data

_COLOR_RESET

_LEVEL_TO_COLOR

logger

API

class nemo_automodel.components.loggers.log_utils.ColorFormatter(
fmt: str | None = None,
datefmt: str | None = None,
use_color: bool = True
)

Bases: Formatter

Logging formatter that colorizes the level name and includes date/time.

The date is included via asctime with a default format of YYYY-MM-DD HH:MM:SS. Colors can be disabled by setting the NO_COLOR env var, or forced with FORCE_COLOR.

use_color
= bool(use_color) and self._stream_supports_color()
nemo_automodel.components.loggers.log_utils.ColorFormatter._stream_supports_color() -> bool
nemo_automodel.components.loggers.log_utils.ColorFormatter.format(
record: logging.LogRecord
) -> str
class nemo_automodel.components.loggers.log_utils.RankFilter()

Bases: Filter

A logging filter that controls log output based on the process rank.

This filter allows log messages only for rank 0 by default.

nemo_automodel.components.loggers.log_utils.RankFilter.filter(
record: logging.LogRecord
) -> bool

Decide whether to log the provided record.

Parameters:

record
logging.LogRecord

The log record to be evaluated.

Returns: bool

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

nemo_automodel.components.loggers.log_utils._ensure_root_handler_with_formatter(
formatter: logging.Formatter
) -> None

Ensure the root logger has at least one StreamHandler with the given formatter.

If handlers already exist on the root logger, set their formatter to the provided formatter. Otherwise, create a StreamHandler, attach the formatter and RankFilter, and add it to the root logger.

nemo_automodel.components.loggers.log_utils.add_filter_to_all_loggers(
filter: typing.Union[logging.Filter, typing.Callable[[LogRecord], bool]]
) -> None

Add a filter to the root logger and all existing loggers.

Parameters:

filter
Union[Filter, Callable[[LogRecord], bool]]

A logging filter instance or callable to add.

nemo_automodel.components.loggers.log_utils.module_filter(
record: logging.LogRecord,
modules_to_filter: list[str]
) -> bool

Logging filter to exclude messages from specific modules.

Parameters:

record
LogRecord

The logging record to check.

modules_to_filter
list[str]

A list of module name prefixes to filter out.

Returns: bool

False if the record’s logger name starts with any of the specified

nemo_automodel.components.loggers.log_utils.setup_logging(
logging_level: int = logging.INFO,
modules_to_filter: typing.Optional[list[str]] = None,
set_level_for_all_loggers: bool = False
) -> None

Set up logging level and filters for the application.

Configures the logging level based on arguments, environment variables, or defaults. Optionally adds filters to suppress messages from specific modules.

Logging Level Precedence:

  1. Env var LOGGING_LEVEL
  2. logging_level argument
  3. Default: logging.INFO

Also configures a colorized formatter that includes the date/time.

nemo_automodel.components.loggers.log_utils._COLOR_RESET = '\x1b[0m'
nemo_automodel.components.loggers.log_utils._LEVEL_TO_COLOR = {logging.DEBUG: '\x1b[36m', logging.INFO: '\x1b[32m', logging.WARNING: '\x1b[33m...
nemo_automodel.components.loggers.log_utils.logger = logging.getLogger(__name__)