找到"日志错误"在使用Python的日志文件中

时间:2018-02-19 09:37:45

标签: python python-3.x python-2.7

我正在尝试创建一个脚本,将日志文件分解为多个列表(ip,日志时间,GET请求,路径,错误)。

我希望我的脚本在我的日志文件中打印正确的错误行,如何让我的脚本查找,查找并打印它?

继承我的剧本:

file_1=open("lolz.txt","r")
file_2=open("lolz2.txt","r")
a = file_1.readlines()
b = file_2.readlines()
date=[]
ipss=[]
post=[]
date2=[]
kind=[]
ipss2=[]
con=[]
erorr=[]
for i in a:
    i = i.split(" - - ")
    for x in i:
        x = x.split("/")
        ipss.append(x[0].strip("[07"))
        i = i[1].split(' ')
        date.append(i[0].strip("[]-0800"))
for line in a:
    q = (line[47::])
    post.append(q.strip("GET"" ""\n"))
for lins in b:
    t = (lins[0:26])
    y = (lins[26:34])
    u = (lins[34:56])
    p = (lins[56:140])
    date2.append(t.strip("[ ]"))
    kind.append(y.strip("[ ]"))
    ipss2.append(u.strip("[ ] ( )"))
    con.append(p.strip("\n - -- ( ) [ ]"))
for poop in b:
    if len(poop) < 138 or len(poop) > 138:
        erorr.append(poop.strip("\n - [ ] ( )"))

记录行示例:

[Sun Mar 7 20:58:27 2004] [info] [client 64.242.88.10] (104)Connection reset by peer: client stopped connection before send body completed
[Sun Mar 7 21:16:17 2004] [error] [client 24.70.56.49] File does not exist: /home/httpd/twiki/view/Main/WebHome
[Sun Mar 7 21:20:14 2004] [info] [client 64.242.88.10] (104)Connection reset by peer: client stopped connection before send body completed

0 个答案:

没有答案