之前转载了一篇关于日志的文章,发现里面的知识点有点多,实际上自己需要的不需要这么多知识点。于是进行下总结。总结后封装单独可以使用的日志类如下,方便以后自己查看使用。
import logging
import os
import datetime
class loggerClass:
#日志级别的字典
level_relations = {"debug": logging.DEBUG, "info": logging.INFO, "warning": logging.WARNING,
"error": logging.ERROR, "critical": logging.CRITICAL,}
#日志输出格式
fmt_str="%(asctime)s - %(name)s - %(levelname)s - %(message)s"
logFile='log' #定义日志存储的文件夹
def __init__(self,level='info',fmt=fmt_str):
if not os.path.exists(self.logFile): #判断日志存储文件夹是否存在,不存在,则新建
os.makedirs(self.logFile)
formatter=logging.Formatter(fmt)
#生成以当天日期为名称的日志文件
filename=self.logFile+'/'+datetime.datetime.today().strftime('%Y-%m-%d')+'.log'
#初始化日志类参数
self.logger=logging