【发布时间】:2014-12-24 07:12:08
【问题描述】:
我正在尝试在日志文件的每一行中搜索特定字符串,如果匹配,我需要能够从该特定错误中获取主机信息。
考虑以下日志条目:
05-05-2014 00:02:02,771 [HttpProxyServer-thread-1314] ERROR fd - Empty user name specified in NTLM authentication. Prompting for auth again.
Host=tools.google.com, Port=80, Client ip=/10.253.168.128, port=37271, User-Agent: Google Update/1.3.23.9;winhttp;cup-ecdsa
05-05-2014 00:02:02,771 [HttpProxyServer-thread-2156] ERROR fd - Empty user name specified in NTLM authentication. Prompting for auth again.
Host=tools.google.com, Port=80, Client ip=/10.253.168.148, port=37273, User-Agent: Google Update/1.3.23.9;winhttp;cup-ecdsa
05-05-2014 00:02:02,802 [HttpProxyServer-thread-604] ERROR fd - Empty user name specified in NTLM authentication. Prompting for auth again.
Host=tools.google.com, Port=80, Client ip=/10.253.168.92, port=37280, User-Agent: Google Update/1.3.23.9;winhttp;cup
这是我的代码:
for line in log_file:
if bool(re.search( r'Empty user name specified in NTLM authentication. Prompting for auth again.', line)):
host = re.search(r'Host=(\D+.\D+.\D+,)', line).group(1)
问题是主机信息与错误不在同一行。它在下一行。我如何让 re.search(r'Host=(\D+.\D+.\D+,)', line).group(1) 在“line”当前所在的下一行中搜索?
【问题讨论】:
-
读取整个文件有什么问题?
-
@AvinashRaj,也许,巨大的日志文件不需要舒适地放在内存中......