import logging
import coloredlogs

FORMAT = "[%(asctime)s] [%(levelname)7s] %(name)7s: %(message)s"


def init(
    name: str = "webtech-admin", level: int = logging.INFO, log_to_file: bool = False
) -> None:
    formatter = coloredlogs.ColoredFormatter(
        fmt=FORMAT,
        field_styles={
            "name": {"color": "red"},
            "levelname": {"color": "magenta"},
            "asctime": {"color": "cyan"},
        },
    )

    sh = logging.StreamHandler()
    sh.setLevel(level)
    sh.setFormatter(formatter)

    handlers: list[logging.Handler] = [sh]

    if log_to_file:
        fh = logging.FileHandler(f"{name}.log")
        fh.setLevel(level)
        fh.setFormatter(formatter)
        handlers.append(fh)

    logging.basicConfig(handlers=handlers, level=level)
