Python记录器转储自"&书信电报;日期>&引用;到新文件
是否有一种优雅的方法来解析python记录器并在时间戳之间创建一个新的日志文件 我们的测试日志文件变得很长,我想通过给它一个时间戳来打破日志,这样我们就有了一个通用日志和一个较小的日志,用于我们要分析的特定时间戳Python记录器转储自"&书信电报;日期>&引用;到新文件,python,python-3.x,python-logging,Python,Python 3.x,Python Logging,是否有一种优雅的方法来解析python记录器并在时间戳之间创建一个新的日志文件 我们的测试日志文件变得很长,我想通过给它一个时间戳来打破日志,这样我们就有了一个通用日志和一个较小的日志,用于我们要分析的特定时间戳 打开日志文件,逐行解析会很费时,我正在搜索一些更苗条的日志文件,因为“2015-01-10 17:15:00”>file.txt def ParseLogger(filepath=r'C:\tmp\LogParser\Test_log_09-09-2020_17-28-49.txt',
打开日志文件,逐行解析会很费时,我正在搜索一些更苗条的日志文件,因为“2015-01-10 17:15:00”>file.txt
def ParseLogger(filepath=r'C:\tmp\LogParser\Test_log_09-09-2020_17-28-49.txt',start='2020-09-09 17:28:54,013',stop='2020-09-09 17:28:56,444',out_name='test'):
'''
:param filepath: source log file
:param start: start time in format '%Y-%m-%d %H:%M:%S,%f'
:param stop: stop time in format '%Y-%m-%d %H:%M:%S,%f'
:param out_name: log name to be added: Log_{out_name}_start-{start}_stop-{stop}.txt
:return: None
'''
try:
logPath = os.path.dirname(filepath)
fileName = f'Log_{out_name}_start-{start.replace(":","")}_stop-{stop.replace(":","")}.txt'
if not os.path.exists(logPath):
os.makedirs(logPath)
LogFile = os.path.join(logPath, fileName)
f = open(LogFile, "w+")
with open(filepath) as fp:
for line in fp:
try:
Logtime = re.findall(r"^\[([0-9 -:,]+)\] ", line, re.MULTILINE)[0]
date_time_obj = datetime.strptime(Logtime, '%Y-%m-%d %H:%M:%S,%f')
if date_time_obj >= datetime.strptime(start, '%Y-%m-%d %H:%M:%S,%f') and date_time_obj <= datetime.strptime(stop, '%Y-%m-%d %H:%M:%S,%f'):
f.write(line)
elif date_time_obj > datetime.strptime(stop, '%Y-%m-%d %H:%M:%S,%f'):
return
except Exception as e:
print("skiped line {} ".format(line))
except Exception as e:
print('Exception: ' + str(e))
exc_type, exc_obj, exc_tb = sys.exc_info()
if exc_type != '' and exc_obj != '' and exc_tb != '':
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print(exc_type, str(e), fname, exc_tb.tb_lineno)
finally:
f.close()
def ParseLogger(filepath=r'C:\tmp\LogParser\Test_log_09-09-2020_17-28-49.txt),start='2020-09-09 17:28:54013',stop='2020-09-09 17:28:56444',out_name='Test':
'''
:param filepath:源日志文件
:param start:start time格式为“%Y-%m-%d%H:%m:%S,%f”
:param stop:stop time格式为“%Y-%m-%d%H:%m:%S,%f”
:param out_name:要添加的日志名:log{out_name}{start-{start}}{stop-{stop}.txt
:返回:无
'''
尝试:
logPath=os.path.dirname(文件路径)
fileName=f'Log{out{u name}{start-{start.replace(“:”,“)}{stop-{stop.replace(“:”,”)}.txt'
如果操作系统路径不存在(日志路径):
os.makedirs(logPath)
LogFile=os.path.join(logPath,文件名)
f=打开(日志文件,“w+”)
打开(文件路径)作为fp时:
对于fp中的行:
尝试:
Logtime=re.findall(r“^\[([0-9-:,]+)\]”,行,re.MULTILINE)[0]
date\u time\u obj=datetime.strtime(日志时间,%Y-%m-%d%H:%m:%S,%f')
如果日期\u时间\u对象>=datetime.strtime(开始,'%Y-%m-%d%H:%m:%S,%f')和日期\u时间\u对象datetime.strtime(停止,'%Y-%m-%d%H:%m:%S,%f'):
返回
例外情况除外,如e:
打印(“跳过行{}”。格式(行))
例外情况除外,如e:
打印('例外:'+str(e))
exc_类型,exc_对象,exc_tb=sys.exc_info()
如果exc_类型!=“”还有exc_obj!“”和exc_tb!='':
fname=os.path.split(exc_tb.tb_frame.f_code.co_文件名)[1]
打印(exc_类型、str(e)、fname、exc_tb.tb_行号)
最后:
f、 关闭()
日志行示例:[2020-09-10 11:21:46109]-[sys.module name]-[INFO]-[some data]sample txt sample txt sample txt你可以从中获得一些启示。基本上扩展
Logger
类,允许它从时间X到时间Y转储日志