Last active
August 29, 2015 13:56
-
-
Save partkyle/9262187 to your computer and use it in GitHub Desktop.
JSON logpp in python
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 datetime | |
import fileinput | |
import json | |
import logging | |
import sys | |
import time | |
logging.basicConfig(level=logging.DEBUG, format='%(timestamp)s [%(app)s:%(process_id)s] %(caller)s - %(message)s') | |
# log processors | |
def raw_processor(line): | |
""" | |
Simply prints out the raw representation of a log message | |
""" | |
print(line.strip()) | |
def _format_json(d, level=1): | |
result = '' | |
keys = sorted(d.keys()) | |
for k in keys: | |
v = d[k] | |
if isinstance(v, dict): | |
v = _format_json(v, level+1) | |
if isinstance(v, list): | |
v = json.dumps(v) | |
result += '\n{0}{1}: {2}'.format(' '*level, k, v) | |
return result | |
def json_processor(line): | |
""" | |
parses the json from the log message and prints it in a pretty format | |
""" | |
data = json.loads(line) | |
extra = { | |
'timestamp': datetime.datetime.fromtimestamp(data.pop('timestamp', int(time.time()))), | |
'app': data.pop('app', None), | |
'process_id': data.pop('process_id', None), | |
'caller': data.pop('caller', None), | |
} | |
message = data.pop('message', 'NO MESSAGE!') | |
json_data = _format_json(data) | |
logging.info('%s %s', message, json_data, extra=extra) | |
def main(processor): | |
for line in fileinput.input(): | |
processor(line) | |
if __name__ == '__main__': | |
main(json_processor) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment