Skip to content

Logging

Structured logging utilities.

Quick Example

from mamba_agents.observability import setup_logging
from mamba_agents.config import LoggingConfig

config = LoggingConfig(
    level="INFO",
    format="json",
    redact_sensitive=True,
)

logger = setup_logging(config)
logger.info("Agent started", model="gpt-4o")

Formats

Text Format

2024-01-15 10:30:00 INFO [agent] Starting agent run

JSON Format

{"timestamp": "2024-01-15T10:30:00", "level": "INFO", "message": "Starting agent run"}

Configuration

Option Type Default Description
level str "INFO" Log level
format str "text" Output format
redact_sensitive bool True Redact secrets

API Reference

setup_logging

setup_logging(
    config: LoggingConfig | None = None,
    name: str = "mamba_agents",
) -> AgentLogger

Set up logging with the given configuration.

PARAMETER DESCRIPTION
config

Logging configuration.

TYPE: LoggingConfig | None DEFAULT: None

name

Logger name.

TYPE: str DEFAULT: 'mamba_agents'

RETURNS DESCRIPTION
AgentLogger

Configured AgentLogger instance.

Source code in src/mamba_agents/observability/logging.py
def setup_logging(
    config: LoggingConfig | None = None,
    name: str = "mamba_agents",
) -> AgentLogger:
    """Set up logging with the given configuration.

    Args:
        config: Logging configuration.
        name: Logger name.

    Returns:
        Configured AgentLogger instance.
    """
    if config is None:
        config = LoggingConfig()

    logger = logging.getLogger(name)
    logger.setLevel(getattr(logging, config.level.upper()))

    # Remove existing handlers
    logger.handlers.clear()

    # Create handler
    if config.file:
        handler: logging.Handler = logging.FileHandler(config.file)
    else:
        handler = logging.StreamHandler(sys.stderr)

    handler.setLevel(getattr(logging, config.level.upper()))

    # Set formatter
    if config.format == "json":
        formatter: logging.Formatter = StructuredFormatter(include_extras=config.include_timestamps)
    else:
        fmt = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
        formatter = logging.Formatter(fmt)

    handler.setFormatter(formatter)

    # Add sensitive data filter if redaction is enabled
    if config.redact_sensitive:
        handler.addFilter(SensitiveDataFilter())

    logger.addHandler(handler)

    return AgentLogger(logger)

AgentLogger

AgentLogger(logger: Logger)

Wrapper around logging.Logger with structured logging support.

Initialize the agent logger.

PARAMETER DESCRIPTION
logger

Underlying logger.

TYPE: Logger

Source code in src/mamba_agents/observability/logging.py
def __init__(self, logger: logging.Logger) -> None:
    """Initialize the agent logger.

    Args:
        logger: Underlying logger.
    """
    self._logger = logger

debug

debug(msg: str, **kwargs: Any) -> None

Log debug message.

Source code in src/mamba_agents/observability/logging.py
def debug(self, msg: str, **kwargs: Any) -> None:
    """Log debug message."""
    self._log(logging.DEBUG, msg, kwargs)

info

info(msg: str, **kwargs: Any) -> None

Log info message.

Source code in src/mamba_agents/observability/logging.py
def info(self, msg: str, **kwargs: Any) -> None:
    """Log info message."""
    self._log(logging.INFO, msg, kwargs)

warning

warning(msg: str, **kwargs: Any) -> None

Log warning message.

Source code in src/mamba_agents/observability/logging.py
def warning(self, msg: str, **kwargs: Any) -> None:
    """Log warning message."""
    self._log(logging.WARNING, msg, kwargs)

error

error(msg: str, **kwargs: Any) -> None

Log error message.

Source code in src/mamba_agents/observability/logging.py
def error(self, msg: str, **kwargs: Any) -> None:
    """Log error message."""
    self._log(logging.ERROR, msg, kwargs)

exception

exception(msg: str, **kwargs: Any) -> None

Log exception with traceback.

Source code in src/mamba_agents/observability/logging.py
def exception(self, msg: str, **kwargs: Any) -> None:
    """Log exception with traceback."""
    self._log(logging.ERROR, msg, kwargs, exc_info=True)