1 self.logger = logging.getLogger(logName) # 创建logger实例 2 time = datetime.datetime.now() 3 logFilePath = os.path.abspath('../log/') + '/{}-{}-{}'.format(time.year, time.month, time.day) # 设置日志文件输出路径 4 self.check_mkdir(logFilePath) 5 logFile = logFilePath + '/out.log' 6 formatter = logging.Formatter('%(asctime)-12s %(levelname)-8s %(name)-10s %(message)-12s') # 设置日志输出格式 7 loghanlder = logging.FileHandler(logFile, encoding='utf-8') # encoding要申明下,不然中文写入中文文件会乱码 8 loghanlder.setFormatter(formatter) 9 loghanlder.setLevel(logging.DEBUG) # 设置日志输出等级10 logSt = logging.StreamHandler()11 logSt.setFormatter(formatter)12 self.logger.addHandler(loghanlder)13 self.logger.addHandler(logSt)