Last active
August 29, 2015 14:25
-
-
Save mzupan/65b43e8531a0eafa8fa3 to your computer and use it in GitHub Desktop.
A simple python script to format json logs getting pipped in from stdin
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
#!/usr/bin/env python | |
import sys | |
import json | |
def print_json(l): | |
try: | |
j = json.loads(l) | |
print json.dumps(j, indent=4, sort_keys=True) | |
print "-" * 50 | |
except: | |
pass | |
try: | |
buff = '' | |
while True: | |
buff += sys.stdin.read(1) | |
if buff.endswith('\n'): | |
print_json(buff[:-1]) | |
buff = '' | |
except KeyboardInterrupt: | |
sys.stdout.flush() | |
pass |
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
chmod +x /path/to/json_print.py | |
tail -f file.log | /path/to/json_print.py | |
Example | |
{ | |
"level": "info", | |
"message": "publishing check request", | |
"payload": { | |
"command": "/usr/lib64/sensu/plugins/check_rabbitmq_cluster.py", | |
"issued": 1438012694, | |
"name": "check_rabbitmq_cluster" | |
}, | |
"subscribers": [ | |
"rabbit" | |
], | |
"timestamp": "2015-07-27T15:58:14.118235+0000" | |
} | |
-------------------------------------------------- | |
{ | |
"level": "info", | |
"message": "publishing check request", | |
"payload": { | |
"command": "/usr/lib64/sensu/plugins/check_devcenter.py", | |
"issued": 1438012694, | |
"name": "check_devcenter" | |
}, | |
"subscribers": [ | |
"monitor" | |
], | |
"timestamp": "2015-07-27T15:58:14.449236+0000" | |
} | |
-------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment