【问题标题】:How to find the log line with the most recent time stamp?如何找到具有最新时间戳的日志行?
【发布时间】:2018-07-18 17:52:55
【问题描述】:

目前我正在遍历目录以查找/存储/显示包含版本号信息的最新日志行。我正在使用 Regex 查找带有版本号的日志行,并且我正在尝试通过将它们与 parse_version 进行比较来找到具有最新时间戳的日志行。

例如,我文件夹的文件中的日志行如下所示:

2018-05-08T15:47:27.752Z 00000000-0000-0000-0000-000000000000 > LVL:2 RC: version: 2.12.1.10452

2018-05-08T21:27:14.2049217Z> <INFO >: Version: 2.10.0.23960

2018-05-08T21:18:53.0428568Z> <INFO >: Version: 2.12.1.26051

这些只是我文件夹文件中数千条日志行的几个示例,我正在尝试查找包含版本号信息的最新日志行。在这种情况下,我希望选择第二行,即使它的版本号较低,因为它具有更新的时间戳。

下面是我的代码,为了简单起见,我没有包含循环遍历文件夹的代码。

            for line in f: #For simplicity sake, I won't include my code above this line because it's just for looping through the folder to find the log lines
            #0strip out \x00 from read content, in case it's encoded differently
            line = line.replace('\x00', '')

            #Regular expressions for finding the log lines in the folder
            RE2 = r"^.+INFO.+Version.+"
            RE3 = r"^.+RC: version"

            previous_version_line = '0'
                version_to_display = '00'
                #Find the general matches, and get the version line with the latest time stamp
                pattern2 = re.compile('('+RE2+'|'+RE3+')', re.IGNORECASE)
                for match2 in pattern2.finditer(line):

                    if parse_version(line) > parse_version(previous_version_line):
                        version_to_display = line
                        previous_version_line = line
                    else:
                        version_to_display = previous_version_line

                    print(version_to_display)

现在问题似乎在于 parse_version 比较,虽然通过正则表达式找到的日志行的值应该高于 0,但 if 语句总是评估为 false,我只是打印一堆 0。

提前致谢!

【问题讨论】:

    标签: regex python-3.x comparison


    【解决方案1】:

    找到其中包含“版本”的每一行,按时间排序并在日志消息中打印最新时间:

    data = """
    2018-05-08T15:47:27.752Z 00000000-0000-0000-0000-000000000000 > LVL:2 RC: version: 2.12.1.10452
    
    2018-05-08T21:27:14.2049217Z> <INFO >: Version: 2.10.0.23960
    
    2018-05-08T21:18:53.0428568Z> <INFO >: Version: 2.12.1.26051
    """
    
    import re
    from datetime import datetime
    
    data_new = []
    for (d, log) in re.findall(r'([\d\-:T\.]+Z)>?\s+(.*)', data):
        if not re.search('version', log, flags=re.I):
            continue
        parts = d.split('.')
        if len(parts[1]) >= 8:
            d = parts[0] + '.' + parts[1][:6] + 'Z'
        data_new.append((datetime.strptime(d, '%Y-%m-%dT%H:%M:%S.%fZ'), log))
    
    data_new = sorted(data_new, reverse=True)
    if data_new:
        t = data_new[0][0].strftime('%Y-%m-%dT%H:%M:%S.%fZ')
        print(f'Latest version to display:\ntime=[{t}] msg=[{data_new[0][1]}]')
    

    打印:

    Latest version to display:
    time=[2018-05-08T21:27:14.204921Z] msg=[<INFO >: Version: 2.10.0.23960]
    

    警告:

    Python datetime 类只接受微秒到 6 位数字(所以这个程序会截断它)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2013-06-24
      相关资源
      最近更新 更多