【问题标题】:log analyze: finding lines by time difference日志分析:按时差查找行
【发布时间】:2012-08-30 14:51:18
【问题描述】:

我生成了一个长日志文件,其中log4j, 10 threads 写入日志。 我正在寻找可以找到用户等待很长时间的行的日志分析器工具(即同一线程的日志条目之间的差异超过一分钟)。

P.S 我正在尝试使用OtrosLogViewer,但它会根据某些值(例如,通过线程 ID)进行过滤,并且不会在行之间进行比较。

PPS 新版本的 OtrosLogViewer 有一个“Delta”列,用于计算 adj log 行之间的差异(以 ms 为单位)

谢谢你

【问题讨论】:

  • 这听起来非常具体。您是否考虑过自己编写一些代码来解决这个问题?或者,我们能否帮助您解决导致您以这种方式检查日志的根本问题?
  • 我认为这是一个共同的需求。当用户等待很长时间时,它有助于找到点
  • OtrosLogViewer 可以过滤/突出显示日志行。您需要比较两条不同的行,因此没有日志查看器可以帮助您

标签: java multithreading logging log-analysis otroslogviewer


【解决方案1】:

这个简单的 Python 脚本可能就足够了。为了测试,我分析了我的本地 Apache 日志,顺便说一句,它使用 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

【讨论】:

    【解决方案2】:

    根据@Raffaele 的回答,我对任何日志文件进行了一些修复(跳过不以请求日期开头的行,例如 Jenkins 控制台日志)。 此外,添加了 Max / Min Threshold 以根据持续时间限制过滤掉行。

    #!/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
    

    【讨论】:

      【解决方案3】:

      Apache Chainsaw 有一个时间增量列。

      【讨论】:

        猜你喜欢
        • 2015-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-22
        • 2011-08-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多