Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example of generating JSON logging output from Python
import json
import logging
import platform
import sys
import time
import traceback
class JSONFormatter:
"""A formatter for the standard logging module that converts a LogRecord into JSON
Output matches JSONLayout from Any
keyword arguments supplied to the constructor are output in a "tags" sub-object.
def __init__(self, **tags):
self.tags = tags
def format(self, record):
result = {
'timestamp': time.strftime("%Y-%m-%dT%H:%M:%S", time.gmtime(record.created)) +
(".%03dZ" % (1000 * (record.created % 1))),
'level': record.levelname,
'message': record.msg % record.args,
'hostname': platform.node(),
'processId': record.process,
'thread': record.threadName,
'locationInfo': {
'fileName': record.filename,
'lineNumber': record.lineno
if self.tags:
result['tags'] = self.tags
if (record.exc_info):
result['exception'] = traceback.format_exception(record.exc_info[0], record.exc_info[1], record.exc_info[2])
return json.dumps(result)
def configure_logging():
handler = logging.StreamHandler(sys.stderr)
logging.basicConfig(level=logging.DEBUG, handlers=[handler])
if __name__ == '__main__':
logger = logging.getLogger(__name__)'Started')
logger.debug('this is a test of %s %s', "value", "substitutions")
raise Exception("example")
except Exception as ex:
logger.exception("caught exception")'Finished')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment