Skip to content

Logging Configuration

Configure the loguru logger with custom settings. Check Loguru Documentation for more details.

Parameters:

Name Type Description Default
format_string str

The format string for log messages.

'<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>'
level str

The minimum logging level. Default: "INFO"

'INFO'
sink Union[PathLike[str], TextIO, BinaryIO]

Where to send the log. Default: sys.stderr

stderr
rotation str

When to rotate the log file. Example: "10 MB", "1 day" Only used when sink is a file path.

None
retention str

How long to keep log files. Example: "1 week", "10 days" Only used when sink is a file path.

None
include_correlation_id bool

Whether to include correlation ID in log messages. Default: True

True

Returns:

Type Description
Logger

The configured logger object

Source code in airia/logs.py
def configure_logging(
    format_string: str = "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
    level: str = "INFO",
    sink: Union[os.PathLike[str], TextIO, BinaryIO] = sys.stderr,
    rotation: Optional[str] = None,
    retention: Optional[str] = None,
    include_correlation_id: bool = True,
) -> "loguru.Logger":
    """
    Configure the loguru logger with custom settings.
    Check [Loguru Documentation](https://loguru.readthedocs.io/en/stable/api/logger.html) for more details.

    Args:
        format_string (str): The format string for log messages.
        level (str): The minimum logging level. Default: "INFO"
        sink: Where to send the log. Default: sys.stderr
        rotation (str, optional): When to rotate the log file.
                                  Example: "10 MB", "1 day"
                                  Only used when sink is a file path.
        retention (str, optional): How long to keep log files.
                                   Example: "1 week", "10 days"
                                   Only used when sink is a file path.
        include_correlation_id (bool): Whether to include correlation ID in log messages.
                                      Default: True

    Returns:
        The configured logger object
    """
    # Remove any existing handlers
    logger.remove()

    # Modify format string to include correlation ID if requested
    if include_correlation_id:
        format_string = "<magenta>[{extra[correlation_id]}]</magenta> " + format_string

    # Add rotation and retention only for file paths
    kwargs = {
        "format": format_string,
        "level": level,
        "filter": correlation_id_filter,  # Add the filter for each handler
    }

    if isinstance(sink, (str, os.PathLike)):
        if rotation is not None:
            kwargs["rotation"] = rotation
        if retention is not None:
            kwargs["retention"] = retention

    # Add the new handler
    logger.add(sink, **kwargs)

    return logger