Skip to content

Logging with any-agent

any-agent comes with a logger powered by Rich

Quick Start

By default, logging is set up for you. But if you want to customize it, you can call:

from any_agent.logging import setup_logger

setup_logger()

Customizing the Logger

View the docstring in setup_logger for a description of the arguments available .

Example: Set Log Level to DEBUG

from any_agent.logging import setup_logger
import logging

setup_logger(level=logging.DEBUG)

Example: Custom Log Format

setup_logger(log_format="%(asctime)s - %(levelname)s - %(message)s")

Example: Propagate Logs

setup_logger(propagate=True)

any_agent.logging.setup_logger(level=logging.ERROR, rich_tracebacks=True, log_format=None, propagate=False, **kwargs)

Configure the any_agent logger with the specified settings.

Parameters:

Name Type Description Default
level int

The logging level to use (default: logging.INFO)

ERROR
rich_tracebacks bool

Whether to enable rich tracebacks (default: True)

True
log_format str | None

Optional custom log format string

None
propagate bool

Whether to propagate logs to parent loggers (default: False)

False
**kwargs Any

Additional keyword arguments to pass to RichHandler

{}
Source code in src/any_agent/logging.py
def setup_logger(
    level: int = logging.ERROR,
    rich_tracebacks: bool = True,
    log_format: str | None = None,
    propagate: bool = False,
    **kwargs: Any,
) -> None:
    """Configure the any_agent logger with the specified settings.

    Args:
        level: The logging level to use (default: logging.INFO)
        rich_tracebacks: Whether to enable rich tracebacks (default: True)
        log_format: Optional custom log format string
        propagate: Whether to propagate logs to parent loggers (default: False)
        **kwargs: Additional keyword arguments to pass to RichHandler

    """
    logger.setLevel(level)
    logger.propagate = propagate

    # Remove any existing handlers
    for handler in logger.handlers[:]:
        logger.removeHandler(handler)

    handler = RichHandler(rich_tracebacks=rich_tracebacks, markup=True, **kwargs)

    if log_format:
        formatter = logging.Formatter(log_format)
        handler.setFormatter(formatter)

    logger.addHandler(handler)