# -*- coding: utf-8 -*-
# @Author: wangkun
# @Time: 2023/7/3
import os
import logging
from datetime import time, date
from logging.handlers import TimedRotatingFileHandler


class Log:
    logger = None
    handler = None

    @classmethod
    def logging(cls, log_type, crawler):
        # 日志路径
        log_dir = f"./{crawler}/logs/"
        log_path = os.getcwd() + os.sep + log_dir
        if not os.path.isdir(log_path):
            os.makedirs(log_path)

        # 日志文件名
        log_name = f"{date.today()}-{crawler}-{log_type}.log"

        if cls.logger is None:
            cls.logger = logging.getLogger(__name__)
            cls.logger.setLevel(logging.INFO)

        if cls.handler is None:
            cls.handler = TimedRotatingFileHandler(
                os.path.join(log_path, log_name),
                when="midnight",
                interval=1,
                atTime=time(hour=0, minute=0, second=0),
                backupCount=20) # 设置保留的日志文件数量
            formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(filename)s - line %(lineno)d - %(message)s')
            cls.handler.setFormatter(formatter)
            cls.logger.addHandler(cls.handler)

        return cls.logger


if __name__ == "__main__":
    Log.log()