import logging标签:logging,log,level,self,msg,封装,my From: https://www.cnblogs.com/huanyingnidedaolai/p/16732689.html
from conf.project_path import *
class Log:
def my_log(self,msg,level):
#定义一个日志收集器
my_logging = logging.getLogger("记录的日志是")
#设定级别
my_logging.setLevel("DEBUG")
#设置格式格式 %(asctime)s - %(name)s - %(levelname)s - %(message)s
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - 日志信息:%(message)s')
#创建输出渠道
ch = logging.StreamHandler()
ch.setLevel("DEBUG")
ch.setFormatter(formatter)
fh = logging.FileHandler(log_path,encoding="utf-8")
fh.setLevel("DEBUG")
fh.setFormatter(formatter)
#对接
my_logging.addHandler(ch)
my_logging.addHandler(fh)
#收集日志
if level == "DEBUG":
my_logging.debug(msg)
elif level == "ERROR":
my_logging.error(msg)
elif level == "INFO":
my_logging.info(msg)
elif level == "WARNING":
my_logging.warning(msg)
elif level == "CRITICAI":
my_logging.critical(msg)
#关闭渠道
my_logging.removeHandler(ch)
my_logging.removeHandler(fh)
def debug(self,msg):
self.my_log(msg,"DEBUG")
def info(self,msg):
self.my_log(msg,"INFO")
def error(self,msg):
self.my_log(msg,"ERROR")
def critical(self,msg):
self.my_log(msg,"CRITICAI")
def warning(self,msg):
self.my_log(msg,"WARNING")
if __name__ == '__main__':
pass
# my_log = Log()
# my_log.debug("不可以")
# my_log.info("可以")
# Log().my_log("周一天气真好1","ERROR")
# Log().my_log("周二天气真好1", "ERROR")
# Log().my_log("周三天气真好1", "INFO")