获取log 方法封装【记录】

# -*- coding: utf-8 -*-
"""
@Time : 2021/11/16 上午9:27
@Auth : 一条咸鱼
@File :get_log.py
@IDE :PyCharm
@Motto:ABC(Always Be Coding)
获取log 的方法封装
"""
import logging.handlers
import os

from config import Base_PATH


class GetLog:
# 新建一个日志变量
__logger = None

# 获取日志的方法
@classmethod
def get_logger(cls):
# 如果日志为空,执行
if cls.__logger is None:
# 获取日志器
cls.__logger = logging.getLogger()
# 修改默认等级
cls.__logger.setLevel(logging.INFO)
# log 保存路径
log_path = Base_PATH + os.sep + "log" + os.sep + "hmtt.log"
# 获取处理器
th = logging.handlers.TimedRotatingFileHandler(filename=log_path,
when="midnight",
interval=1,
backupCount=3,
encoding="utf-8")

# 获取格式器
fmt = "%(asctime)s %(levelname)s [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s"
fm = logging.Formatter(fmt)
# 将格式器添加到处理器中
th.setFormatter(fm)
# 将处理器添加到日志器中
cls.__logger.addHandler(th)
# 返回日志器
return cls.__logger

if __name__ == '__main__':
GetLog.get_logger()
posted @ 2021-11-17 09:41  测试小歘歘  阅读(58)  评论(0编辑  收藏  举报