import time import logging from logging.handlers import TimedRotatingFileHandler def log_file_namer(log_file_name): log_file_name_split = log_file_name.split('.') log_suffix = log_file_name_split.pop(1) log_file_name_split.append(log_suffix) return '.'.join(log_file_name_split) if __name__ == '__main__': logging.basicConfig(level=1) logger = logging.getLogger('xxxx') file_handler = TimedRotatingFileHandler('text.log', when='S', backupCount=5) # 从TimedRotatingFileHandler点进去看源码, # 判断日志文件是否过期需要备份的方法为doRollover, # 找到备份文件命名的方法self.rotation_filename, # 点进去发现如果提供self.namer方法可以自定义备份文件名称, # 故此提供一个namer方法就可以自定义备份文件名称了 file_handler.namer = log_file_namer logger.addHandler(file_handler) for _ in range(10): time.sleep(1) logger.info(f'{_}', hhh)
标签:logging,name,自定义,python,namer,split,file,log From: https://www.cnblogs.com/shulongshuyue/p/18284827