Blog

Cost effective solutions

Productive meetings with fast deliveries

24/7 support with sameday response

Django Logging

Posted by vladimir gorea on Wed 03 April 2019 Updated on Sun 05 July 2020

Intro

Setting up logging is crucial to monitoring any app. This way you know what works and what doesn't. If done correctly it will save a lot of time of debugging.

Django implementation of logging

Django uses the pyhton logging module. Concepts to understand are loggers, handlers, filters and formatters:

  1. Loggers are messages containers
  2. Handlers are directing loggers to various output destinations like files, console, email, etc
  3. Filters can be used for both loggers and handlers
  4. Formatters handle how to output the messages. Belong to handlers

Basic usage

Set up logging in settings.py

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'verbose': {
            'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
            'style': '{',
        },
        'simple': {
            'format': '{levelname} {message}',
            'style': '{',
        },
    },
    'handlers': {
        'console': {
            'class': 'logging.StreamHandler',
            'formatter': 'verbose'
        },
    },
    'root': {
        'handlers': ['console'],
        'level': 'WARNING',
    },
    'django': {
        'handlers': ['console'],
        'level': 'DEBUG',
        'propagate': True,
    },
    'someapp.urls': {
        'handlers': ['console'],
        'level': 'DEBUG',
        'propagate': True,
    }
}

Set up logging in your app:

import logging
logger = logging.getLogger(__name__)

Resources for reading

These resources are great for understanding why and how to use logging in Django:

  1. django logging the right way
  2. django and logging in plain english
  3. django logging in production

What’s your Project About?

We would love to know about your line of business and how we can help you grow!
Tell Us About Your Project