python基础之logging
import logging def logger(): logger = logging.getLogger('logger')#括号里面默认是root logger.setLevel('DEBUG')#设置等级是DEBUG,数字是10,INFO是20,WARNING是30,ERROR是40,CRITICLE是50 _format = logging.Formatter('[%(name)s] %(asctime)s %(levelname)-8s: %(message)s') fh = logging.FileHandler('log.txt',encoding='utf-8')#文件处理器 ch = logging.StreamHandler()#控制台处理器 logger.addHandler(fh) logger.addHandler(ch) fh.setFormatter(_format)#设置文件格式 ch.setFormatter(_format)#设置控制台输出格式 return logger def add(a,b): print('开始运行add函数') logger.warning(f'a = {a},b={b}') sumer = a + b logger.info(f'结果={sumer}') return sumer logger = logger() add(1,4)