Last active
September 1, 2017 07:18
-
-
Save htv2012/8e8d70274975799061cdc1be443cc59f to your computer and use it in GitHub Desktop.
How to log to JSON
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 json | |
import logging | |
class JSONFormatter: | |
""" | |
A simple formatter class which takes a record and return the fields | |
in JSON format | |
""" | |
def __init__(self, json_encoder=None): | |
self.json_encoder = json_encoder or json.JSONEncoder() | |
def format(self, record): | |
return self.json_encoder.encode(record.__dict__) | |
class JSONHandler(logging.StreamHandler): | |
""" | |
A JSON handler which writes each record as a dictionary | |
""" | |
def __init__(self, stream=None): | |
super(JSONHandler, self).__init__(stream) | |
self.before_first_record = True | |
def emit(self, record): | |
""" | |
For each log record (AKA line), emit as JSON | |
""" | |
self.acquire() | |
if self.before_first_record: | |
self.stream.write('[\n') | |
else: | |
self.stream.write(',\n') | |
self.before_first_record = False | |
self.stream.write(self.format(record)) | |
self.flush() | |
self.release() | |
def close(self): | |
""" | |
Write the closing part of the JSON | |
""" | |
self.acquire() | |
self.stream.write('\n]\n') | |
self.flush() | |
self.release() | |
def get_logger(name, level=logging.WARN, json_encoder=None): | |
""" | |
Convenient factory function to create a new logger which employs | |
the JSONHandler | |
""" | |
logger = logging.getLogger(__name__) | |
logger.setLevel(level) | |
handler = JSONHandler() | |
formatter = JSONFormatter(json_encoder=json_encoder) | |
handler.setFormatter(formatter) | |
logger.addHandler(handler) | |
return logger | |
def main(): | |
logger = get_logger( | |
__name__, | |
level=logging.DEBUG, | |
json_encoder=json.JSONEncoder(indent=4, sort_keys=True)) | |
# Start logging | |
logger.info('Preparing breakfast') | |
logger.warn('Almost out of coffee') | |
logger.error( | |
'error: %s, line %d, file %s', | |
'out of milk', 51, 'cow.java') | |
logger.debug('This is debug') | |
if __name__ == '__main__': | |
main() |
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
[ | |
{ | |
"args": [], | |
"created": 1504232368.014854, | |
"exc_info": null, | |
"exc_text": null, | |
"filename": "json_logging.py", | |
"funcName": "main", | |
"levelname": "INFO", | |
"levelno": 20, | |
"lineno": 51, | |
"module": "json_logging", | |
"msecs": 14.853954315185547, | |
"msg": "Preparing breakfast", | |
"name": "__main__", | |
"pathname": "/Volumes/haibo/cloudstation/src/python/logging/json_logging.py", | |
"process": 884, | |
"processName": "MainProcess", | |
"relativeCreated": 0.5948543548583984, | |
"thread": 140735977423808, | |
"threadName": "MainThread" | |
}, | |
{ | |
"args": [], | |
"created": 1504232368.015089, | |
"exc_info": null, | |
"exc_text": null, | |
"filename": "json_logging.py", | |
"funcName": "main", | |
"levelname": "WARNING", | |
"levelno": 30, | |
"lineno": 52, | |
"module": "json_logging", | |
"msecs": 15.089035034179688, | |
"msg": "Almost out of coffee", | |
"name": "__main__", | |
"pathname": "/Volumes/haibo/cloudstation/src/python/logging/json_logging.py", | |
"process": 884, | |
"processName": "MainProcess", | |
"relativeCreated": 0.8299350738525391, | |
"thread": 140735977423808, | |
"threadName": "MainThread" | |
}, | |
{ | |
"args": [ | |
"out of milk", | |
51, | |
"cow.java" | |
], | |
"created": 1504232368.0153, | |
"exc_info": null, | |
"exc_text": null, | |
"filename": "json_logging.py", | |
"funcName": "main", | |
"levelname": "ERROR", | |
"levelno": 40, | |
"lineno": 53, | |
"module": "json_logging", | |
"msecs": 15.30003547668457, | |
"msg": "error: %s in line %d, file %s", | |
"name": "__main__", | |
"pathname": "/Volumes/haibo/cloudstation/src/python/logging/json_logging.py", | |
"process": 884, | |
"processName": "MainProcess", | |
"relativeCreated": 1.0409355163574219, | |
"thread": 140735977423808, | |
"threadName": "MainThread" | |
}, | |
{ | |
"args": [], | |
"created": 1504232368.015451, | |
"exc_info": null, | |
"exc_text": null, | |
"filename": "json_logging.py", | |
"funcName": "main", | |
"levelname": "DEBUG", | |
"levelno": 10, | |
"lineno": 54, | |
"module": "json_logging", | |
"msecs": 15.45095443725586, | |
"msg": "This is debug", | |
"name": "__main__", | |
"pathname": "/Volumes/haibo/cloudstation/src/python/logging/json_logging.py", | |
"process": 884, | |
"processName": "MainProcess", | |
"relativeCreated": 1.191854476928711, | |
"thread": 140735977423808, | |
"threadName": "MainThread" | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment