Skip to content

Instantly share code, notes, and snippets.

@portante
Created March 14, 2017 22:25
Show Gist options
  • Select an option

  • Save portante/83467850acf8fec984ded3b2f829dfe8 to your computer and use it in GitHub Desktop.

Select an option

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
#!/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