import logging

class ColoredFormatter(logging.Formatter):
    """Colored formatter for the logging package."""

    def __init__(
        self, fmt=None, datefmt=None, style="%", validate=True, *, defaults=None
    ):
        """Colored formatter for the logging package."""
        fmt = fmt or "%(levelname)s: %(message)s"
        super().__init__(fmt, datefmt, style, validate, defaults=defaults)
        colors = {
            "red": "\x1b[31;20m",
            "bold_red": "\x1b[31;1m",
            "green": "\x1b[32;20m",
            "bold_green": "\x1b[32;1m",
            "yellow": "\x1b[33;20m",
            "bold_yellow": "\x1b[33;1m",
            "blue": "\x1b[34;20m",
            "bold_blue": "\x1b[34;1m",
            "grey": "\x1b[37;20m",
            "bold_grey": "\x1b[37;1m",
            "reset": "\x1b[0m",
        }
        self._default_formatter = logging.Formatter(fmt)
        self._formatters = {
            100: logging.Formatter(colors["bold_blue"] + fmt + colors["reset"]),
            logging.DEBUG: logging.Formatter(colors["grey"] + fmt + colors["reset"]),
            logging.INFO: logging.Formatter(colors["green"] + fmt + colors["reset"]),
            logging.WARNING: logging.Formatter(
                colors["yellow"] + fmt + colors["reset"]
            ),
            logging.ERROR: logging.Formatter(colors["red"] + fmt + colors["reset"]),
            logging.CRITICAL: logging.Formatter(
                colors["bold_red"] + fmt + colors["reset"]
            ),
        }

    def format(self, record):
        """Override of logging.Formatter.format"""
        return self._formatters.get(record.levelno, self._default_formatter).format(
            record
        )
