Last active
January 2, 2016 10:39
-
-
Save Widdershin/8291740 to your computer and use it in GitHub Desktop.
Concatenates all log files in directory into one file, sorted by timestamp.Usage: logsplat.py directory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Concatenates all log files in directory into one file, sorted by timestamp. | |
Usage: logsplat.py directory | |
""" | |
import collections | |
import glob | |
import os | |
from operator import attrgetter | |
import re | |
import sys | |
STAMP_REGEX = re.compile(r"(?P<timestamp>[\d\/]{10} [\d:\.]{12}) (?P<content>.*)") | |
FILENAME = "splat.log" | |
Line = collections.namedtuple("Line", ["timestamp", "content", "file"]) | |
prettify_line = "[{0.file}] {0.timestamp} {0.content}".format | |
def get_log_files(): | |
log_files = glob.iglob('*.log') | |
try: | |
log_files.remove(FILENAME) | |
except ValueError: | |
pass | |
return log_files | |
def read_lines(log_file): | |
def construct_line_from_match(match): | |
start_of_filename = log_file[:8] | |
return Line(file=start_of_filename, **match.groupdict() | |
with open(log_file) as open_log: | |
lines = list(open_log) | |
line_regex_matches = [] | |
for line in lines: | |
match = STAMP_REGEX.match(line) | |
if match: | |
line_regex_matches.append(match) | |
return (construct_line_from_match(match) for match in line_regex_matches) | |
def main(): | |
directory = sys.argv[1] | |
os.chdir(directory) | |
log_files = get_log_files() | |
lines = [read_lines(log_file) for log_file in log_files] | |
lines = sorted(lines, key=attrgetter('timestamp')) | |
with open(FILENAME, 'w') as splat_file: | |
splat_file.write("\n".join(map(prettify_line, lines))) | |
print("Wrote {} lines to {}".format(len(lines), FILENAME)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment