Skip to content

Instantly share code, notes, and snippets.

@AdrianAcala
Last active January 13, 2023 22:31
Show Gist options
  • Save AdrianAcala/f0ea0686478eddd2eec0d1193d277785 to your computer and use it in GitHub Desktop.
Save AdrianAcala/f0ea0686478eddd2eec0d1193d277785 to your computer and use it in GitHub Desktop.
Merges log files together but keeps lines with a tab together with the previous line
import argparse
import heapq
def merge_log_files(input_files, output_file):
with open(output_file, 'w') as out_file:
for line in heapq.merge(*(open(f,'r') for f in input_files)):
current_block = []
if line.startswith("\t"):
current_block.append(line)
else:
if current_block:
for block_line in current_block:
out_file.write(block_line)
out_file.write(line)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Merge log files while keeping stack traces intact')
parser.add_argument('input_files', nargs='+', help='Input log files to be merged')
parser.add_argument('output_file', help='Output file to save the merged logs')
args = parser.parse_args()
merge_log_files(args.input_files, args.output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment