# -*- coding: utf-8 -*-
# 日志打印类
# 作者: 陈磊
# 时间: 2019-10-21

import logging
import datetime
import os

from logging.handlers import RotatingFileHandler
from WorkBase import WorkBase

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
# 日志格式
log_format = "%(asctime)s - %(levelname)s - %(message)s"
log_datefmt = "%Y-%m-%d %H:%M:%S"
fmt = logging.Formatter(log_format, log_datefmt)
# 控制台日志
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
sh.setFormatter(fmt)
# 文件日志
year = str(datetime.datetime.now().year) + u"年"
month = str(datetime.datetime.now().month) + u"月"
day = str(datetime.datetime.now().day) + u"日"

# 导入日志文件路径
path = WorkBase().log_path + year + "\\" + month + "\\" + day + "\\"
if os.path.exists(path):
    pass
else:
    os.makedirs(path)
logger_name = str(datetime.datetime.today().strftime("%Y%m%d%H%M%S")) + ".txt"
log_path = path + logger_name
# fh = logging.FileHandler(log_path)
# 根据日志文件大小分割 最多9999份
fh = RotatingFileHandler(log_path, maxBytes=8*1024*1024, backupCount=9999)
fh.setLevel(logging.DEBUG)
fh.setFormatter(fmt)
# 添加日志处理器
logger.addHandler(sh)
logger.addHandler(fh)


class UtilsLogOld:
    def __init__(self):
        self.logger = logger
        self.path = path
        self.logger_name = logger_name

    def info(self, message):
        return self.logger.info(message)

    def debug(self, message):
        return self.logger.debug(message)

    def warning(self, message):
        return self.logger.warning(message)

    def error(self, message):
        return self.logger.error(message)
