Python 从“<date>”到新文件的记录器转储

Python logger dump since "<date>" to new file

有没有一种优雅的方法来解析 python 记录器并创建一个新的时间戳记日志文件?

我们的测试日志文件变得很长,我想通过给它一个时间戳来打破日志,这样我们就会有一个通用日志和一个较小的日志,用于我们想要分析的某个时间戳。

打开日志文件并逐行解析它会很耗时,我正在搜索比 journalctl --since "2015-01-10 17:15:00" > file.txt 命令更靠谱的东西

这是我可以执行此操作的代码:

def ParseLogger(filepath=r'C:\tmp\LogParser\Test_log_09-09-2020_17-28-49.txt',start='2020-09-09 17:28:54,013',stop='2020-09-09 17:28:56,444',out_name='test'):
'''
:param filepath: source log file
:param start: start time in format '%Y-%m-%d %H:%M:%S,%f'
:param stop: stop time in format '%Y-%m-%d %H:%M:%S,%f'
:param out_name: log name to be added: Log_{out_name}_start-{start}_stop-{stop}.txt
:return: None
'''
try:
    logPath = os.path.dirname(filepath)
    fileName = f'Log_{out_name}_start-{start.replace(":","")}_stop-{stop.replace(":","")}.txt'
    if not os.path.exists(logPath):
        os.makedirs(logPath)
    LogFile = os.path.join(logPath, fileName)
    f = open(LogFile, "w+")
    with open(filepath) as fp:
        for line in fp:
            try:
                Logtime = re.findall(r"^\[([0-9 -:,]+)\] ", line, re.MULTILINE)[0]
                date_time_obj = datetime.strptime(Logtime, '%Y-%m-%d %H:%M:%S,%f')
                if date_time_obj >= datetime.strptime(start, '%Y-%m-%d %H:%M:%S,%f') and date_time_obj <= datetime.strptime(stop, '%Y-%m-%d %H:%M:%S,%f'):
                    f.write(line)
                elif date_time_obj > datetime.strptime(stop, '%Y-%m-%d %H:%M:%S,%f'):
                    return
            except Exception as e:
                print("skiped line  {} ".format(line))
except Exception as e:
        print('Exception: ' + str(e))
        exc_type, exc_obj, exc_tb = sys.exc_info()
        if exc_type != '' and exc_obj != '' and exc_tb != '':
            fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
            print(exc_type, str(e), fname, exc_tb.tb_lineno)
finally:
    f.close()

日志行示例:[2020-09-10 11:21:46,109] - [sys.module name] - [INFO] - [some data] sample txt sample txt sample txt sample txt