#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = logging工具类 __Time__ = 2021/11/29 """ import logging import sys import time from logging import handlers class Loggers: __instance = None def __new__(cls, *args, **kwargs): if not cls.__instance: cls.__instance = object.__new__(cls, *args, **kwargs) return cls.__instance def __init__(self): # 设置输出格式 formater = logging.Formatter( '[%(asctime)s]-[%(levelname)s]-[%(filename)s]-[%(funcName)s:%(lineno)d] : %(message)s') # 定义一个日志收集器 self.logger = logging.getLogger('log') # 设定级别 self.logger.setLevel(logging.DEBUG) now = time.strftime( '%Y%m%d%H', time.localtime(time.time())) + '.log' execfile = sys.argv[0] logfilename = execfile.replace(".py", now) # 输出渠道一 - 文件形式 self.fileLogger = handlers.RotatingFileHandler(logfilename, maxBytes=5242880, backupCount=3, encoding="utf-8") # 输出渠道二 - 控制台 self.console = logging.StreamHandler() # 控制台输出级别 self.console.setLevel(logging.DEBUG) # 输出渠道对接输出格式 self.console.setFormatter(formater) self.fileLogger.setFormatter(formater) # 日志收集器对接输出渠道 self.logger.addHandler(self.fileLogger) self.logger.addHandler(self.console) # def debug(self, msg): # self.logger.debug(msg=msg) # # def info(self, msg): # self.logger.info(msg=msg) # # def warn(self, msg): # self.logger.warning(msg=msg) # # def error(self, msg): # self.logger.error(msg=msg) # # def excepiton(self, msg): # self.logger.exception(msg=msg) def getLog(self): return self.logger