在日志文件中搜索唯一日期(python)

时间:2015-05-29 21:16:48

标签: python

我正在编写一个脚本来解析日志文件并匹配某些字符串,如“INFO”,“WARN”,“SEVERE”等。

我可以使用下面的代码解决这个问题。

from sys import argv
from collections import OrderedDict

# Find and catalog each log line that matches these strings
match_strings = ["INFO", "WARN", "SEVERE"]

if len(argv) > 1:
    files = argv[1:]
else:
    print "ERROR: You must provide at least one log file to be processed."
    print "Example:"
    print "%s my.log" % argv[0]
    exit(2)

for filename in files:
    with open(filename) as f:
        data = f.read().splitlines()
        # Create data structure to handle results
        matches = OrderedDict()
        for string in match_strings:
            matches[string] = []

    for i, s in enumerate(data, 1):
        for string in match_strings:
            if string in s:
                matches[string].append('Line %03d: %s' % (i, s,))
for string in matches:
    print "\"%s\": %d" % (string, len(matches[string]))

日志文件如下:

2014-05-26T15:06:14.597+0000 INFO...
2014-05-26T15:06:14.597+0000 WARN...
2014-05-27T15:06:14.597+0000 INFO...
2014-05-28T15:06:14.597+0000 SEVERE...
2014-05-29T15:06:14.597+0000 SEVERE...

当前输出如下:

"INFO": 2
"WARN": 1
"SEVERE": 2

然而,我宁愿做的是让脚本整理并按日期打印格式化输出。因此,不是打印一个简单的列表(上面),我们可以使用上面的示例得到类似下面的内容:

Category    2014-05-26  2014-05-27  2014-05-28  2014-05-29

"INFO":         1           1           0           0
"WARN":         1           0           0           0
"SEVERE":       0           0           1           1   

是否有任何想法/建议如何实现?

1 个答案:

答案 0 :(得分:0)

这样做的一种方法是创建一个包含变量info,warn和severe的类。然后创建一个字典,其中每个元素都是这个类,其中键是日期。然后,在解析日志文件时,您只需找到日期并将其用作字典的索引,并根据需要增加信息,警告和严重。