#_author:star
#date:2019/11/6
#logger----->模块级别的函数
#文件与屏幕同时显示日志信息
import logging
logger=logging.getLogger()
#创建一个Handler,用于写入日志文件
fh=logging.FileHandler('logger.txt')
#再创建一个Handler,用于输出到控制台
sh=logging.StreamHandler()
formatter=logging.Formatter('%(asctime)s-%(filename)s-[line:%(lineno)d]-%(levelname)s-%(message)s')
fh.setFormatter(formatter)
sh.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(sh)
logger.setLevel(logging.DEBUG)
logger.debug('debug message11')
logger.info('info message11')
logger.warning('warning message11')
logger.error('error message')
logger.critical('critical message11')
Output:
![](https://img2018.cnblogs.com/common/1844053/201911/1844053-20191106223411725-1238552621.png)