Skip to content

Logging

logging

Logging configuration for t3api_utils.

Sets up a Rich-based logging handler with colored output and rich tracebacks. Logging is configured automatically on import; the level is read from the LOG_LEVEL environment variable (default: INFO). Call :func:setup_logging to reconfigure with a different level.

setup_logging

setup_logging(level: int = -1) -> None

Configure the root logger with a Rich handler.

Replaces any existing handlers on the root logger with a single :class:rich.logging.RichHandler that renders rich tracebacks and supports Rich markup in log messages.

Parameters:

Name Type Description Default
level int

Logging level threshold. When -1 (the default sentinel), the level is resolved from the LOG_LEVEL environment variable (falling back to logging.INFO).

-1
Source code in t3api_utils/logging.py
def setup_logging(level: int = -1) -> None:
    """Configure the root logger with a Rich handler.

    Replaces any existing handlers on the root logger with a single
    :class:`rich.logging.RichHandler` that renders rich tracebacks and
    supports Rich markup in log messages.

    Args:
        level: Logging level threshold. When ``-1`` (the default sentinel),
            the level is resolved from the ``LOG_LEVEL`` environment variable
            (falling back to ``logging.INFO``).
    """
    if level == -1:
        level = _resolve_log_level()
    logging.basicConfig(
        level=level,
        format="%(message)s",
        datefmt="[%X]",
        handlers=[RichHandler(rich_tracebacks=True, markup=True)]
    )

get_logger

get_logger(name: str) -> logging.Logger

Return a named logger.

Parameters:

Name Type Description Default
name str

Logger name, typically __name__ of the calling module.

required

Returns:

Name Type Description
A Logger

class:logging.Logger instance for the given name.

Source code in t3api_utils/logging.py
def get_logger(name: str) -> logging.Logger:
    """Return a named logger.

    Args:
        name: Logger name, typically ``__name__`` of the calling module.

    Returns:
        A :class:`logging.Logger` instance for the given name.
    """
    return logging.getLogger(name)