Creating custom log handler that logs to database models in django

By | September 19, 2012

I was told to create some kind of logging mechanism to insert logs into database, and make sure it's generic. So after some thought I decided to mimic the builtin RotatingFileHandler and write a DBHandler.

The RotatingFileHandler allows you to specify which file to write to and rotate files, therefore my DBHandler should also allow you to specify which model to insert to and specify expiry in settings, and best of all, on a standalone app.

My implementation uses a separate database for logging, which needs have a router like this:

of course you need to tell settings you have 2 databases like this:

and we need to create some models:

then we need to create a custom handler, call logging.handlers.DBHandler

and go back to settings to specify the custom handler to log stuff like this

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.