import logging
import sys


class ColorFormatter(logging.Formatter):
    COLORS = {
        logging.DEBUG: "\033[36m",  # cyan
        logging.INFO: "\033[32m",  # green
        logging.WARNING: "\033[33m",  # yellow
        logging.ERROR: "\033[31m",  # red
        logging.CRITICAL: "\033[41m",  # red background
    }
    RESET = "\033[0m"

    def format(self, record: logging.LogRecord) -> str:
        color = self.COLORS.get(record.levelno, "")
        levelname = record.levelname
        record.levelname = f"{color}{levelname}{self.RESET}"
        msg = super().format(record)
        record.levelname = levelname  # restore
        return msg


def get_logger(
    name: str = "gremux",
    level: int = logging.INFO,
) -> logging.Logger:
    """
    Get a logger object from the standard logging library that
    formats the text according to ColorFormatter

    Parameters:
    ----------
    * `name`: str
    * `level`: int
    """

    logger = logging.getLogger(name)
    logger.setLevel(level)

    # prevent duplicate handlers if called multiple times
    if logger.handlers:
        return logger

    handler = logging.StreamHandler(sys.stdout)
    formatter = ColorFormatter(fmt="[%(levelname)s] %(message)s")
    handler.setFormatter(formatter)

    logger.addHandler(handler)
    logger.propagate = False
    return logger
