import logging class MyLog(): def my_log(self,msg,level): #定义一个日志收集器 my_logger my_logger=logging.getLogger("龙腾电竞") #设定级别 my_logger.setLevel("DEBUG") #设置输出格式 %开头 (日志类别)s - formatter=logging.Formatter('%(asctime)s-%(levelname)s-%(filename)s-%(name)s-日志信息:%(message)s') #创建一个自己的输出渠道 ch=logging.StreamHandler() #日志输出到控制台 ch.setLevel("ERROR") #收集等级,想收集什么等级就设置什么 ch.setFormatter(formatter) fh=logging.FileHandler("liudada.txt",encoding="utf-8") #日志输出到控制台 fh.setLevel("DEBUG") fh.setFormatter(formatter) #两者对接(收集器和渠道)------>指定收集渠道 my_logger.addHandler(ch) ch.setFormatter(formatter) my_logger.addHandler(fh) fh.setFormatter(formatter) #收集日志 if level =="DEBUG": my_logger.debug(msg) elif level =="INFO": my_logger.info(msg) elif level =="WARNING": my_logger.warning(msg) elif level =="ERROR": my_logger.error(msg) elif level == "CTITICAL": my_logger.critical(msg) #关闭渠道 my_logger.removeHandler(ch) my_logger.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") if __name__ == '__main__': pass # my_logger=MyLog() # my_logger.debug("可不可以这样??") # MyLog().my_log("AAAdddd","ERROR") # MyLog().my_log("AAAAssssss","ERROR") # MyLog().my_log("AAAA大大和ssss","ERROR")