Created
March 14, 2017 22:25
-
-
Save portante/83467850acf8fec984ded3b2f829dfe8 to your computer and use it in GitHub Desktop.
Simple Python program to process log files that have unsorted JSON documents, one per line
This file contains hidden or 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
| #!/usr/bin/env python | |
| import os, sys, json | |
| from operator import itemgetter | |
| lines = [] | |
| try: | |
| with open(sys.argv[1], "r") as fp: | |
| for line in fp.readlines(): | |
| doc = json.loads(line) | |
| msg = doc['message'] | |
| if msg.endswith('\n'): | |
| doc['message'] = msg[:-1] | |
| lines.append(doc) | |
| except Exception as e: | |
| print e | |
| sys.exit(1) | |
| # sort lines by @timestamp field | |
| sorted_lines = sorted(lines, key=itemgetter('@timestamp')) | |
| for doc in sorted_lines: | |
| #print json.dumps(doc, indent=4) | |
| print "%-30s %-20s %-30s %s" % (doc['@timestamp'], doc['hostname'], doc['kubernetes']['pod_name'], doc['message']) | |
| sys.exit(0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment