日志文件中过去 60 分钟内出现的正则表达式字符串



>我正在尝试在日志文件中搜索过去 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],我无法安装任何其他模块或升级。

下面的代码片段应该会有所帮助。您还需要将微秒添加到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

最新更新