Outputting JSON in Django logs (98% of the way there!)

1,750 views
Skip to first unread message

Shawn Milochik

unread,
Mar 8, 2012, 12:24:31 PM3/8/12
to django...@googlegroups.com
Hi everyone. I'm hoping someone has gotten this working and can point
out whatever tiny thing I'm doing wrong here.

I want to log JSON instead of the default. I found JsonFormatter here:
https://github.com/madzak/python-json-logger

It works great with a small script I created based on the example:

>>> import logging
>>> import jsonlogger
>>>
>>> logger = logging.getLogger()
>>> handler = logging.StreamHandler()
>>> formatter = jsonlogger.JsonFormatter('%(levelname)s %(asctime)s
%(module)s %(process)d %(message)s %(pathname)s $(lineno)d $(funcName)s')
>>>
>>> handler.setFormatter(formatter)
>>> logger.addHandler(handler)
>>> logger.setLevel(logging.DEBUG)
>>>
>>> logger.debug('Hello Django Users!')
{"process": 9261, "module": "<stdin>", "funcName": "<module>",
"pathname": "<stdin>", "lineno": 1, "asctime": ["12-03-08
12:18:05,486366"], "message": "Hello Django Users!", "levelname": "DEBUG"}

However, when I try to use it in Django, I just get the "message"
portion as a string, no JSON or anything.

Here's how I configured it:

1. Imported import jsonlogger in settings.py.

2. Added the following to the 'formatters' section of the LOGGING
dictionary (based on instructions here:
http://www.python.org/dev/peps/pep-0391/#user-defined-objects):

'json': {
(): jsonlogger.JsonFormatter,
'fmt': '%(levelname)s %(asctime)s %(module)s %(process)d
%(message)s %(pathname)s $(lineno)d $(funcName)s',
},


3. Changed the 'formatter' dict key of the handlers in the 'handlers'
section of the LOGGING dict to 'json' (the name of the formatter).

Note: I've tried leaving out the 'fmt' kwarg in the formatter and
accepting the default, with no change in result.

Thanks,
Shawn

Shawn Milochik

unread,
Mar 8, 2012, 2:32:36 PM3/8/12
to django...@googlegroups.com
It turns out I was just missing quotes around the () for the custom formatter.

I just wrote a blog post will a full working example in case anyone else is interested.



Reply all
Reply to author
Forward
0 new messages