Skip to content

Logging

Centralized logging configuration for PyAgenity.

This module provides logging configuration that can be imported and used throughout the project. Each module should use:

import logging
logger = logging.getLogger(__name__)

This ensures proper hierarchical logging with module-specific loggers.

Typical usage example

from pyagenity.utils.logging import configure_logging configure_logging(level=logging.DEBUG)

Functions:

Name Description
configure_logging

Configures the root logger for the PyAgenity project.

Functions

configure_logging

configure_logging(level=logging.INFO, format_string=None, handler=None)

Configures the root logger for the PyAgenity project.

This function sets up logging for all modules under the 'pyagenity' namespace. It ensures that logs are formatted consistently and sent to the appropriate handler.

Parameters:

Name Type Description Default

level

int

Logging level (e.g., logging.INFO, logging.DEBUG). Defaults to logging.INFO.

INFO

format_string

str

Custom format string for log messages. If None, uses a default format: "[%(asctime)s] %(levelname)-8s %(name)s: %(message)s".

None

handler

Handler

Custom logging handler. If None, uses StreamHandler to stdout.

None

Returns:

Type Description
None

None

Example

configure_logging(level=logging.DEBUG) logger = logging.getLogger("pyagenity.module") logger.info("This is an info message.")

Source code in pyagenity/utils/logging.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
def configure_logging(
    level: int = logging.INFO,
    format_string: str | None = None,
    handler: logging.Handler | None = None,
) -> None:
    """
    Configures the root logger for the PyAgenity project.

    This function sets up logging for all modules under the 'pyagenity' namespace.
    It ensures that logs are formatted consistently and sent to the appropriate handler.

    Args:
        level (int, optional): Logging level (e.g., logging.INFO, logging.DEBUG).
            Defaults to logging.INFO.
        format_string (str, optional): Custom format string for log messages.
            If None, uses a default format: "[%(asctime)s] %(levelname)-8s %(name)s: %(message)s".
        handler (logging.Handler, optional): Custom logging handler. If None,
            uses StreamHandler to stdout.

    Returns:
        None

    Raises:
        None

    Example:
        >>> configure_logging(level=logging.DEBUG)
        >>> logger = logging.getLogger("pyagenity.module")
        >>> logger.info("This is an info message.")
    """
    if format_string is None:
        format_string = "[%(asctime)s] %(levelname)-8s %(name)s: %(message)s"

    if handler is None:
        handler = logging.StreamHandler(sys.stdout)

    formatter = logging.Formatter(format_string)
    handler.setFormatter(formatter)

    # Configure root logger for pyagenity
    root_logger = logging.getLogger("pyagenity")
    root_logger.setLevel(level)

    # Only add handler if none exists to avoid duplicates
    if not root_logger.handlers:
        root_logger.addHandler(handler)

    # Prevent propagation to avoid duplicate logs
    root_logger.propagate = False