import logging import time import os log_path = os.path.join((os.path.dirname(os.path.dirname(os.path.realpath(__file__)))),'logs') a=os.path.exists(log_path) if a == False:#判断a路径是否存在,不存在则创建一个路径文件夹 os.mkdir(log_path) class Log: def __init__(self): # 初始化创建一个带时间戳的log文件 self.logname = os.path.join(log_path, '{0}.log'.format(time.strftime('%Y-%m-%d'))) def __printconsole(self, level, message): # 创建一个logger logger = logging.getLogger() logger.setLevel(logging.DEBUG) # 创建一个handler,用于写入日志文件 fh = logging.FileHandler(self.logname,'a',encoding='utf-8') fh.setLevel(logging.DEBUG) # 再创建一个handler,用于输出到控制台 ch = logging.StreamHandler() ch.setLevel(logging.DEBUG) # 定义handler的输出格式 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) ch.setFormatter(formatter) # 给logger添加handler logger.addHandler(fh) logger.addHandler(ch) # 记录一条日志 if level == 'info': logger.info(message) elif level == 'debug': logger.debug(message) elif level == 'warning': logger.warning(message) elif level == 'error': logger.error(message) logger.removeHandler(ch) logger.removeHandler(fh) # 关闭打开的文件 fh.close() def debug(self,message): self.__printconsole('debug', message) def info(self,message): self.__printconsole('info', message) def warning(self,message): self.__printconsole('warning', message) def error(self,message): self.__printconsole('error', message)