Last active
July 8, 2024 07:30
-
-
Save kdgregory/82cc3942311c1983a9e141a8ced1f5fd to your computer and use it in GitHub Desktop.
Example of generating JSON logging output from 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
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 https://github.com/kdgregory/log4j-aws-appenders. 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, | |
'logger': record.name, | |
'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) | |
handler.setFormatter(JSONFormatter(application="example")) | |
logging.basicConfig(level=logging.DEBUG, handlers=[handler]) | |
if __name__ == '__main__': | |
configure_logging() | |
logger = logging.getLogger(__name__) | |
logger.info('Started') | |
logger.debug('this is a test of %s %s', "value", "substitutions") | |
try: | |
raise Exception("example") | |
except Exception as ex: | |
logger.exception("caught exception") | |
logger.info('Finished') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment