我正在尝试在最后60分钟内在日志文件中搜索字符串。
我为其设置了两个变量,然后尝试从日志文件中找到这两者之间的字符串 -
current_time = datetime.datetime.now() ## 3/13/18 13:17:31
past_time = datetime.timedelta(minutes=60) ## 3/13/18 12:17:31
因为字符串(SESN0066E)发生在两个字符串[声明的日期时间戳]
之间 def format_time(t):
s = datetime.datetime.strptime(str(t),'%Y-%m-%d %H:%M:%S.%f')
formattedTime = s.strftime('%m/%d/%y %H:%M')
return formattedTime
start_time=format_time((datetime.datetime.now() - datetime.timedelta(minutes=60)))
current_time=format_time(datetime.datetime.now())
with open('server.log') as my_log:
a = [re.findall(r'\[('+str(start_time)+')\](.*?)\[('+str(current_time)+')\]+',line) for line in my_log.readlines() if 'SESN0066E:' in line]
print a
此处我的服务器日志文件采用以下格式 -
[3/13/18 10:31:18:360 CET] 0000000a WXSProperties I SESN0066E: The value of the "com.ibm.websphere.objectgrid.container.heartbeat.any
form" property is "true".
[3/13/18 13:17:31:615 CET] 00000078 webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception:
[3/13/18 13:17:31:633 CET] 00000082 webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception The server cannot use the error page specified for your application because of the exception printed below.
[3/13/18 13:17:31:635 CET] 00000082 webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception:
[3/13/18 13:17:31:707 CET] 000000a8 webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception The server cannot use the error page specified for your application because of the exception printed below.
[3/13/18 13:17:31:709 CET] 000000a8 webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception:
[3/13/18 13:17:31:856 CET] 0000007c webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception The server cannot use the error page specified for your application because of the exception printed below.
[3/13/18 13:17:31:800 CET] 0000000a WXSProperties I SESN0066E: The value of the "com.ibm.websphere.objectgrid.container.heartbeat.any
form" property is "true"
[3/13/18 13:17:31:858 CET] 0000007c webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception:
[3/13/18 13:17:31:872 CET] 000000aa webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception The server cannot use the error page specified for your application because of the exception printed below.
这里只在current_time(3/13/18 13:17:31)和past_time(3/13/18 12:17:31)之间发现一次 -
预期结果:
['[3/13/18 13:17:31:800 CET] 0000000a WXSProperties I SESN0066E: The value of the "com.ibm.websphere.objectgrid.container.heartbeat.any
form" property is "true"']
或
[[3/13/18 13:17:31:800 CET]]
但是我得到空列表,不知道我在这里缺少什么。 [Python 2.6],我无法安装任何其他模块或升级。
答案 0 :(得分:0)
以下代码段应该有所帮助。您还需要将微秒添加到current_time & past_time
。
import re
import datetime
s = """[3/13/18 10:31:18:360 CET] 0000000a WXSProperties I SESN0066E: The value of the "com.ibm.websphere.objectgrid.container.heartbeat.any
form" property is "true".
[3/13/18 13:17:31:615 CET] 00000078 webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception:
[3/13/18 13:17:31:633 CET] 00000082 webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception The server cannot use the error page specified for your application because of the exception printed below.
[3/13/18 13:17:31:635 CET] 00000082 webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception:
[3/13/18 13:17:31:707 CET] 000000a8 webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception The server cannot use the error page specified for your application because of the exception printed below.
[3/13/18 13:17:31:709 CET] 000000a8 webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception:
[3/13/18 13:17:31:856 CET] 0000007c webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception The server cannot use the error page specified for your application because of the exception printed below.
[3/13/18 13:17:31:800 CET] 0000000a WXSProperties I SESN0066E: The value of the "com.ibm.websphere.objectgrid.container.heartbeat.any
form" property is "true"
[3/13/18 13:17:31:858 CET] 0000007c webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception:
[3/13/18 13:17:31:872 CET] 000000aa webapp E com.ibm.ws.webcontainer.webapp.WebApp reportRecursiveError Error Page Exception The server cannot use the error page specified for your application because of the exception printed below."""
current_time = datetime.datetime.strptime("3/13/18 13:17:31:800", '%m/%d/%y %H:%M:%S:%f' )
past_time = datetime.datetime.strptime("3/13/18 12:17:31:800", '%m/%d/%y %H:%M:%S:%f')
res = []
for i in s.split("\n"):
if "SESN0066E" in i:
val = i[i.find("[")+1:i.find("]")][:-3].strip()
t = datetime.datetime.strptime(val, '%m/%d/%y %H:%M:%S:%f')
if past_time <= t <= current_time: #Check if log time is between past_time & current_time
print i
print val
<强>输出:强>
[3/13/18 13:17:31:800 CET] 0000000a WXSProperties I SESN0066E: The value of the "com.ibm.websphere.objectgrid.container.heartbeat.any
3/13/18 13:17:31:800