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

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to