我有一个用log4j生成的很长的日志文件,10 threads写入日志。我正在寻找日志分析器工具,可以找到用户等待了很长时间的行(即同一线程的日志条目之间的差异超过一分钟)。
附注:我正在尝试使用OtrosLogViewer,但它通过某些值(例如,通过线程ID)进行过滤,并且不在行之间进行比较。
PPS新版本的OtrosLogViewer有一个“增量”列,用于计算调整日志行之间的差异(以毫秒为单位)
谢谢
发布于 2012-09-08 05:18:04
这个简单的Python脚本可能就足够了。为了进行测试,我分析了我的本地Apache日志,其中BTW使用Common Log Format,因此您甚至可以按原样重用它。我只是简单地计算两个后续请求之间的差值,并打印超出某个阈值(在我的测试中为1秒)的增量的请求行。您可能希望将代码封装在一个函数中,该函数还接受带有线程ID的参数,以便可以进一步过滤
#!/usr/bin/env python
import re
from datetime import datetime
THRESHOLD = 1
last = None
for line in open("/var/log/apache2/access.log"):
# You may insert here something like
# if not re.match(THREAD_ID, line):
# continue
# Python does not support %z, hence the [:-6]
current = datetime.strptime(
re.search(r"\[([^]]+)]", line).group(1)[:-6],
"%d/%b/%Y:%H:%M:%S")
if last != None and (current - last).seconds > THRESHOLD:
print re.search('"([^"]+)"', line).group(1)
last = current发布于 2016-05-01 19:07:15
基于@Raffaele的回答,我做了一些修复来处理任何日志文件(跳过不以请求日期开头的行,例如Jenkins控制台日志)。此外,增加了最大/最小阈值,根据时长限制过滤出行。
#!/usr/bin/env python
import re
from datetime import datetime
MIN_THRESHOLD = 80
MAX_THRESHOLD = 100
regCompile = r"\w+\s+(\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d).*"
filePath = "C:/Users/user/Desktop/temp/jenkins.log"
lastTime = None
lastLine = ""
with open(filePath, 'r') as f:
for line in f:
regexp = re.search(regCompile, line)
if regexp:
currentTime = datetime.strptime(re.search(regCompile, line).group(1), "%Y-%m-%d %H:%M:%S")
if lastTime != None:
duration = (currentTime - lastTime).seconds
if duration >= MIN_THRESHOLD and duration <= MAX_THRESHOLD:
print ("#######################################################################################################################################")
print (lastLine)
print (line)
lastTime = currentTime
lastLine = line
f.closed发布于 2022-01-18 16:08:52
https://stackoverflow.com/questions/12199663
复制相似问题