通过日期来过滤日志,最好的办法范围蟒蛇(The best way to filter a log b

2019-09-23 05:26发布

什么是打印匹配的日期时间范围的日志行的最好方式。 例如:

我想从只打印线,日期:2012/09 / 30-00:00:10:2012/09 / 30-00:00:13

2012/09/30-00:00:08.773 log error
2012/09/30-00:00:09.034 log warning
2012/09/30-00:00:09.352 log info
2012/09/30-00:00:10.526 log info
2012/09/30-00:00:10.995 log warning
2012/09/30-00:00:12.014 log warning
2012/09/30-00:00:18.035 log error
2012/09/30-00:00:21.733 log fatal
2012/09/30-00:00:21.981 log info

它应该打印:

2012/09/30-00:00:10.526 log line
2012/09/30-00:00:10.995 log line
2012/09/30-00:00:12.014 log line

我想这样做的成本效益的方式,因为我使用的生产服务器。 Python的只有请。 谢谢!

Answer 1:

Actullay,日志格式允许未经其转化为比较日期字符串datetime

with open('mylog.log','r') as f:
    for line in f:
        d = line.split(" ",1)[0] 
        if d >= '2012/09/30-00:00:10' and d <= '2012/09/30-00:00:13':
            print line


Answer 2:

假设你逐行读取日志行:

import re
for line in log:
    if re.match("2012/09/30-00:00:1[0-3]", line):
        print line


Answer 3:

我不知道对性能的影响(我怀疑添的回答可能会更快),但这种方法适用于任何日期范围:

>>> def dates(start,end):
...     for i in range(int((end-start).seconds)):
...         yield start + datetime.timedelta(seconds=i)
...
>>> fmt = '%Y/%m/%d-%H:%M:%S'
>>> from_date = datetime.datetime.strptime('2012/09/30-00:00:10',fmt)
>>> till_date = datetime.datetime.strptime('2012/09/30-00:00:13',fmt)
>>> with open('file.log') as f:
...     for line in f:
...         if datetime.datetime.strptime(line.split()[0][:-4],fmt) in dates(fro
m_date,till_date):
...              print line
...
2012/09/30-00:00:10.526 log info
2012/09/30-00:00:10.995 log warning
2012/09/30-00:00:12.014 log warning


Answer 4:

.startswith()例 :

prefixes = tuple("2012/09/30-00:00:1%d" % i for i in range(3))
with open('mylog.log', 'rb') as file:
    print ''.join(line for line in file if line.startswith(prefixes)),

您可以通过使用一个静态的前缀,再后来在测试中使用正则表达式或预选线datetime对象优化。

如果线被日期在输入排序; 你可以早一点攻破而不读取整个文件。



Answer 5:

按照蒂姆假设你正在读一个日志文件,一行一行,然后使用itertools

from itertools import dropwhile, takewhile

from_dt, to_td = '2012/09/30-00:00:10', '2012/09/30-00:00:13'
with open('logfile') as fin:
    of_interest = takewhile(lambda L: L <= to_td, dropwhile(lambda L: L < from_dt, fin))
    for line in of_interest:
        print line


文章来源: The best way to filter a log by a dates range in python