import logging


class Logger:
    logger = None
    levels = {
        "CRITICAL": logging.CRITICAL, "FATAL": logging.FATAL, "ERROR": logging.ERROR, "WARNING": logging.WARNING,
        "WARN": logging.WARN, "INFO": logging.INFO, "DEBUG": logging.DEBUG, "NOTSET": logging.NOTSET,
        "D": logging.DEBUG, "I": logging.INFO, "W": logging.WARNING, "E": logging.ERROR,
        "F": logging.FATAL
    }

    def __init__(self, name: str = '', level='DEBUG', fh: dict = None, ch: dict = None):
        if fh and (not "format_style" in fh.keys()) and (not '%' in fh["format"]):
            fh["format_style"] = '{'
        if ch and (not "format_style" in ch.keys()) and (not '%' in ch["format"]):
            ch["format_style"] = '{'
        self.logger = logging.getLogger(name)
        self.logger.setLevel(self.levels[level])

        if fh:
            fhandler = logging.FileHandler(filename=fh["filename"], mode=fh["mode"], encoding='utf-8')
            fhandler.setLevel(self.levels[fh["level"]])
            fhandler.setFormatter(
                logging.Formatter(fmt=fh["format"], style=['%', fh["format_style"]]["format_style" in fh.keys()]))
            self.logger.addHandler(fhandler)
        if ch:
            chandler = logging.StreamHandler()
            chandler.setLevel(self.levels[ch["level"]])
            chandler.setFormatter(
                logging.Formatter(fmt=ch["format"], style=['%', ch["format_style"]]["format_style" in ch.keys()]))
            self.logger.addHandler(chandler)

        self.d = self.logger.debug
        self.i = self.logger.info
        self.w = self.logger.warning
        self.e = self.logger.error
        self.f = self.logger.fatal
        self.c = self.logger.critical


if __name__ == '__main__':
    log = Logger(name='test', level='DEBUG',
                 fh=None and {
                     "level": 'DEBUG',
                     "format": '{asctime} - {name} - {levelname[0]}: {message}',
                     "filename": './test.log',
                     "mode": 'a'
                 },
                 ch={
                     "level": 'DEBUG',
                     "format": '{asctime} - {name} - {levelname[0]}: {message}'
                 })
    log.d("调试")
    log.i("信息")
    log.w("警告")
    log.e("错误")
    log.f("致命")
    log.c("致命")
