Remote logging with Python logging and Django

June 9th, 2009 by tobias

As part of my work on EveryWatt, our fledgling energy monitoring web site, I needed a way to consolidate log messages from all the data loggers we have running in a single place. If you’re not familiar with it, Python’s logging module is good stuff and worth checking out. We already used it for logging to files locally, and the module defines an HTTPHandler that can deliver log messages to a remote server via HTTP.

To implement the Django side, I wrote a lightweight pluggable app to receive the log messages and store them in the database. To use the app, just create an HTTPHandler that points to your Django site, and add it to a logger:

1
2
3
4
5
6
7
8
9
10
import logging
import logging.handlers
logger = logging.getLogger('mylogger')
http_handler = logging.handlers.HTTPHandler(
    'django.app.hostname:port',
    '/remotelog/your_app_slug/log/',
    method='POST',
)
logger.addHandler(http_handler)
logger.info('testing remote logging')

On the Django side, navigate to /admin/remotelog/logmessage/ and you should have a nice interface (courtesy of the Django admin) to filter, search, and sort log messages as they come in. The app is called django-remotelog, and it’s up on Google code. Check it out, and feel free to comment.

2 Responses to “Remote logging with Python logging and Django”

  1. Dave Says:

    Tried out remotelog, and it works great. Only problem I’m having is getting exception traceback info to show up. I see the view is formating exc_text, but I can’t get the Python logger to put anything in there. Is there some trick to that? Thanks.

  2. tobias Says:

    If I recall correctly exception info will only show up if you call logger.exception('msg') from your application. See:

    http://docs.python.org/library/logging.html#logging.Logger.exception

Leave a Reply