Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (
Help us improve Python packaging - Donate today!

A python logging handler that saves logs into django model. That's it.

Project Description

A python logging handler that saves logs into django model. That’s it.


Via pip:

pip install django-modelhandler


Add modelhandler to your INSTALLED_APPS, then, configure log handler in your desired way. For example, using dictConfig:

    'version': 1,
    'handlers': {
        'modelhandler': {
            'class': 'modelhandler.handlers.LogModel',
            'level': 'ERROR'
    'loggers': {
        'my_logger': {
            'handlers': ['modelhandler'],
            'level': 'ERROR'

Run migrations that will create a Log model:

python migrate

And now you can start logging in django model.

Getting logs:

from modelhandler.models import Log
# Get the latest log
log = Log.objects.latest() # logger name
log.level # logging level integer
log.levelname # logging level as string (DEBUG, INFO, etc.)
log.message # the log message
log.traceback # traceback, if exists. default: None
log.filename # filename (with ext) where the log was sent
log.funcName # function name where the log was sent
log.created # log creation datetime
log.formatted # the log message as if it was written in file. (with [datetime] [level] etc.)

If you have a django admin enabled, then you can browse your logs on model Log of application modelhandler. It has a customized modeladmin to enabale filtering by logger name and levelname, and searching by message.

If you would like to customize a log model (to alter models Meta), then just subclass a modelhandler.models.Log model, do whatever you want and add your model to LogModel handler parameters:

'handlers': {
    'modelhandler': {
        'class': 'modelhandler.handlers.LogModel',
        'model': ''
        'level': 'ERROR'

If you using celery in your project then you might want to add some model cleaning tasks in CELERYBEAT_SCHEDULE:

    'cleanup_day': {
        'task': 'modelhandler.tasks.cleanup_day',
        'schedule': timedelta(days=1)
    }, # OR
    'cleanup_week': {
        'task': 'modelhandler.tasks.cleanup_week',
        'schedule': timedelta(days=7)
    }, # OR
    'cleanup_month': {
        'task': 'modelhandler.tasks.cleanup_month',
        'schedule': timedelta(days=30)

modelhandler.tasks.cleanup_day will delete all logs that are older than one day from time of task execution. modelhandler.tasks.cleanup_week and modelhandler.tasks.cleanup_month are similar.

If you want to customize the time of deletion, there is a task modelhandler.tasks.cleanup_logs that accepts a before parameter that must be a datetime object or None (in this case a value of will be taken). There is no magic: just Log.objects.filter(created__lte=before).delete()


1.0.0 (2016-06-12)

  • Initial release

Release History

This version
History Node


Download Files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, Size & Hash SHA256 Hash Help File Type Python Version Upload Date
(6.6 kB) Copy SHA256 Hash SHA256
Source None Jun 15, 2016

Supported By

Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Google Google Cloud Servers