进行了自定义日志模块 ,可以直接进行定义名称,必须要有个日志文件为logs,生成的日志文件 (eg:filename_20200102_info.log)
class MyLog():
# 自定义日志记录
def __init__(self, filename,):
self.filename = filename
def __logPath(self):
# 日志文件必须为logs文件名
return os.path.join(BASE_DIR, "logs")
def __infoFilePath(self):
# 文件名按天进行分类
return f"{self.filename}_{time.strftime('%Y%m%d', time.localtime(time.time()))}_info.log"
def __errorFilePath(self):
return f"{self.filename}_{time.strftime('%Y%m%d', time.localtime(time.time()))}_error.log"
def __warningFilePath(self):
return f"{self.filename}_{time.strftime('%Y%m%d', time.localtime(time.time()))}_warning.log"
def __logContent(self, content):
rq = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
return "[%s ]: %s"%(rq, content)
def __wirteFile(self, path, content):
# 写入
log_content = self.__logContent(content)
with open(path, "a", errors="ignore", encoding='utf-8') as logw:
logw.write(log_content+"\n")
return True
def info(self, content):
abs_path = os.path.join(self.__logPath(), self.__infoFilePath())
self.__wirteFile(abs_path, content)
return True
def error(self, content):
abs_path = os.path.join(self.__logPath(), self.__errorFilePath())
self.__wirteFile(abs_path, content)
return True
def warning(self, content):
abs_path = os.path.join(self.__logPath(), self.__warningFilePath())
self.__wirteFile(abs_path, content)
return True