#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
__title__ = '日志格式化器'

@Time    : 2020/9/25 13:36
@Author  : zhengwangeng
@Software: PyCharm

# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神兽保佑    ┣┓
                ┃　永无BUG！   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛
"""

import logging
import sys

from logging.handlers import TimedRotatingFileHandler


class LoggerService:

    @staticmethod
    def logger_timefile(log_file, log_name, backupCount=10):
        logger = logging.getLogger(log_name)
        logger.setLevel(logging.DEBUG)
        # format
        formatter = logging.Formatter(fmt='%(asctime)s - %(filename)s[%(lineno)d] - %(levelname)s - %(message)s',
                                      datefmt='%Y/%m/%d %H:%M:%S')

        # StreamHandler
        stream_handler = logging.StreamHandler(sys.stdout)
        stream_handler.setFormatter(formatter)
        logger.addHandler(stream_handler)

        # 创建TimedRotatingFileHandler对象
        file_handler2 = TimedRotatingFileHandler(filename=log_file, when="D", interval=1, backupCount=backupCount)
        file_handler2.setFormatter(formatter)
        logger.addHandler(file_handler2)

        return logger

    @staticmethod
    def logger_file(log_file, log_name):
        logger = logging.getLogger(log_name)
        logger.setLevel(logging.DEBUG)
        # format
        formatter = logging.Formatter(fmt='%(asctime)s - %(filename)s[%(lineno)d] - %(levelname)s - %(message)s',
                                      datefmt='%Y/%m/%d %H:%M:%S')

        # StreamHandler
        stream_handler = logging.StreamHandler(sys.stdout)
        stream_handler.setFormatter(formatter)
        logger.addHandler(stream_handler)

        # # FileHandler
        file_handler = logging.FileHandler(log_file)
        file_handler.setFormatter(formatter)
        logger.addHandler(file_handler)

        return logger
