Skip to content

orchestr8.logger

Logger class that can be inherited for class based logging.

class ClassName(Logger, format="<level>{message}</level>"):
    def __init__(self):
        self.logger.info("Class initialized")

    @classmethod
    def cls_method(cls):
        cls.logger.info("Class method called")

ClassName()
ClassName.cls_method()
ClassName.logger.info("Hello Logger")
Source code in orchestr8/logger.py
class Logger:
    """
    Logger class that can be inherited for class based logging.

    ```python
    class ClassName(Logger, format="<level>{message}</level>"):
        def __init__(self):
            self.logger.info("Class initialized")

        @classmethod
        def cls_method(cls):
            cls.logger.info("Class method called")

    ClassName()
    ClassName.cls_method()
    ClassName.logger.info("Hello Logger")
    ```
    """

    logger: LoguruLogger

    @classmethod
    def __init_subclass__(cls, *, format: str | None = None):  # noqa: A002
        """
        Args:
            format: Logging format to use.
        """
        logger.add(
            sys.stdout,
            format=format or DEFAULT_LOGGER_FORMAT,
            filter=lambda record: record["extra"].get("class_name") == cls.__name__,
        )
        cls.logger = logger.bind(class_name=cls.__name__).opt(colors=True)  # type: ignore[assignment]