我有这个日志文本文件:
omer| (stmt : 0) | adminT| Connection id - 0
omer| (stmt : 0) | adminT| Start Time - 2018-11-06 16:52:01
omer| (stmt : 0) | adminT| Statement create or replace table amit (x date);
omer| (stmt : 0)| adminT| Connection id - 0 - Executing - create or replace table amit (x date);
omer| (stmt : 0) | adminT| Connection id - 0
omer| (stmt : 0) | adminT| End Time - 2018-11-06 16:52:01
omer| (stmt : 0) | adminT| SQL - create or replace table amit (x date);
omer| (stmt : 0) | adminT| Success
admin| (stmt : 1) | adminT| Connection id - 0
admin| (stmt : 1) | adminT| Start Time - 2018-11-06 16:52:14
admin| (stmt : 1) | adminT| Statement create or replace table amit (x int, y int);
admin| (stmt : 1)| adminT| Connection id - 0 - Executing - create or replace table amit (x int, y int);
admin| (stmt : 1) | adminT| Connection id - 0
admin| (stmt : 1) | adminT| End Time - 2018-11-06 16:52:15
admin| (stmt : 2) | adminT| Connection id - 0
admin| (stmt : 2) | adminT| Start Time - 2018-11-06 16:52:19
admin| (stmt : 2) | adminT| Statement create table amit (x int, y int);
admin| (stmt : 2) | adminT| Connection id - 0
admin| (stmt : 2) | adminT| End Time - 2018-11-06 16:52:22
admin| (stmt : 2) | adminT| SQL - Can't create table 'public.amit' - a table with the same name already exists
admin| (stmt : 2) | adminT| Failed
现在我想知道从开始日期到结束日期之间的差值(可以在行尾看到), 接下来我想知道语句是否成功(标记为失败或成功)。 然后我想计算从开始时间到结束时间的增量,这是我实现的代码:
def parse_log_file(log_file):
print(len(""))
my_path = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(my_path, log_file)
max_delta = 0
with open(path, 'r') as f:
lines = f.readlines()[1:]
for line in lines:
elements = line.split('|')
# strip the lines of surrounding spaces
elements = [t.strip() for t in elements]
statement_id = elements[6]
if "Start Time" in elements[8] and statement_id in elements[6]:
start_date = get_date_parsed(elements[8])
if "End Time" in elements[8] and statement_id in elements[6]:
end_date = get_date_parsed(elements[8])
date_time_start_obj = datetime.datetime.strptime(start_date, '%Y-%m-%d %H:%M:%S')
date_time_end_obj = datetime.datetime.strptime(end_date, '%Y-%m-%d %H:%M:%S')
delta = date_time_end_obj - date_time_start_obj
if delta.seconds > max_delta:
max_delta = delta
print(max_delta)
print("hello")
def get_date_parsed(date_str):
res = date_str.split(' ')[3] + ' ' + date_str.split(' ')[4]
return res
现在我想知道是否有办法知道下一行是否包含“Success”,以便日期计算有效
这里有一个基于一组正则表达式的解决方案——每个正则表达式对应您要寻找的模式。最后,我将所有数据存储在一个数据框中以供分析
您的数据的结果是:
更新代码以匹配完整日志格式,如下所示:
代码:
包含完整日志文件的结果:
相关问题 更多 >
编程相关推荐