自"<date>"到新文件以来的 Python 记录器转储



有没有一种优雅的方法来解析python记录器并在不同的时间戳之间创建新的日志文件?

我们的测试日志文件变得很长,我想通过给它一个时间戳来打破日志,这样我们就有了一个通用的日志和一个较小的日志来分析某个时间戳。

打开日志文件并逐行解析它将非常耗时,因为我正在搜索比journalctl --since "2015-01-10 17:15:00" > file.txt命令更精简的内容

这是我可以做到这一点的代码:

def ParseLogger(filepath=r'C:tmpLogParserTest_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()

日志行示例:〔2020-09-10 11:21:46109〕-〔sys.module name〕-〔INFO〕-〔some data〕sample txt sample txt sample txt

最新更新