import os.path
import logging
import time


class Logger(object):

    def __init__(self, log_name, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.log_name = log_name
        self.logger = logging.getLogger(log_name)
        self.logger.handlers = []
        self.init_logger()

    def init_logger(self):
        self.logger.setLevel(logging.DEBUG)
        rq = time.strftime('%Y%m%d', time.localtime(time.time()))

        log_path = os.path.dirname(os.path.abspath('..')) + '/logs/'
        log_name = log_path + rq + '.log'
        fh = logging.FileHandler(log_name)
        fh.setLevel(logging.INFO)

        ch = logging.StreamHandler()
        ch.setLevel(logging.INFO)

        formatter = logging.Formatter(f'[%(asctime)s] - [%(levelname)7s] - [%(name){max(len(self.log_name),15)}s] - [%(filename)20s] - [%(lineno)4s] - %(message)s')
        fh.setFormatter(formatter)
        ch.setFormatter(formatter)

        self.logger.addHandler(fh)
        self.logger.addHandler(ch)

    def get_logger(self):
        return self.logger



