# -*- coding: utf-8 -*- import logging from logging import handlers # 创建一个日志记录文件,名称是testlog logger = logging.getLogger('testlog') # 设置日志级别 logger.setLevel(logging.INFO) # 创建一个RotatingFileHandler,用来轮转日志文件 th = handlers.RotatingFileHandler(filename='testlog.log', mode='w', encoding='utf-8') # 设置日志格式 fmt = '[%(asctime)s] %(levelname)s %(message)s' formatter = logging.Formatter(fmt) th.setFormatter(formatter) # 将处理器添加到日志记录器 logger.addHandler(th) # # 创建一个输出日志到控制台的StreamHandler # hdr = logging.StreamHandler() # hdr.setFormatter(formatter)