Thursday, March 8, 2012

Using JSON logging in Django (and Python in general)

For a while now, I've wanted to output JSON in my Django log files instead of plain text. However, Google searches and the Python and Django documentations fail to provide a single example that contains all of the required bits of information required to make it work.

Specifically, you need to know how to define a custom formatter in a dictionary config for a logger. All the examples are in UML or use the old-style method of instantiating a formatter and assigning it to a handler.

This makes use of the python-json-logger, available here:

https://github.com/madzak/python-json-logger

So, here is a working example. Simply do this in your own code and you'll be all set. Note that in Django you don't have to deal with the dictConfig at all -- just put the right things in your dictionary and Django will do the rest.

#!/usr/bin/env python


import logging
from logging.config import dictConfig
import jsonlogger


log_config = {
    'version': 1,


    'formatters': {
        'json': {
            '()': 'jsonlogger.JsonFormatter',
            'fmt': '%(levelname)s %(asctime)s %(message)s',
        },
    },


    'handlers': {
        'stream': {
            'level': 'DEBUG',
            'formatter': 'json',
            'class': 'logging.StreamHandler',
        },
    },


    'loggers':  {
        'my_logger': {
        'handlers': ['stream'],
        'level': 'DEBUG',
        'propagate': True,
        },
    }


}


dictConfig(log_config)
logger = logging.getLogger('my_logger')


logger.debug('foo')