"""
Logger
======

The Logger class provides shortcuts for colorized logs generated by objects
and the CLI.
"""

import logging
from datetime import datetime
from click import secho, style


colors = {
    40: "red", # ERROR
    30: "yellow", # WARNING
    20: "cyan", # INFO
    10: "white", # DEBUG
    15: "green", # SUCCESS
}


class StreamHandler(logging.StreamHandler):
    pass


class ColorFormatter(logging.Formatter):

    def format(self, record):
        color = colors.get(record.levelno)
        record.levelname = style(record.levelname, fg=color, bold=True)
        record.name = style(record.name, bold=True)
        return super().format(record)

    def formatTime(self, record, datefmt=None):
        return super().formatTime(record, datefmt='%Y-%m-%d %H:%M:%S')


class Logger(logging.Logger):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        handler = StreamHandler()
        formatter = ColorFormatter("%(asctime)s [%(levelname)s] %(name)s - %(message)s")
        handler.setFormatter(formatter)
        self.addHandler(handler)

    def okay(self, msg, *args, **kwargs):
        self.log(15, msg, *args, **kwargs)
