betty.logging - Betty 0.4.0a4 documentation

Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
Toggle table of contents sidebar

# Source code for betty.logging

    """
    Provide logging utilities.
    """

    import sys
    from logging import (
        CRITICAL,
        ERROR,
        WARNING,
        INFO,
        DEBUG,
        NOTSET,
        StreamHandler,
        LogRecord,
    )

    from typing_extensions import override

    class CliHandler(
        StreamHandler,  # type: ignore[type-arg]
    ):
        """
        A logging handler for Betty's Command Line Interface.
        """

        COLOR_LEVELS = {
            CRITICAL: 91,
            ERROR: 91,
            WARNING: 93,
            INFO: 92,
            DEBUG: 97,
            NOTSET: 97,
        }

        def __init__(self):
            StreamHandler.__init__(self, sys.stderr)

        @override
        def format(self, record: LogRecord) -> str:
            s = StreamHandler.format(self, record)
            for level, color in self.COLOR_LEVELS.items():
                if record.levelno >= level:
                    return self._color(s, color)
            return self._color(s, self.COLOR_LEVELS[NOTSET])

        def _color(self, s: str, color: int) -> str:
            return "\033[%dm%s\033[0m" % (color, s)