Django Logging

Every application developed has some bugs and errors. It is the responsibility of the developers to maintain the application by making it bug-free and error-free. To debug the application code and resolve those errors, developers need. The traditional way of debugging the application code is using the print() statements to understand the flow of code. Thus, Django has an important, useful, and underused functionality called Logging that helps us identifying when and where the error has occurred.

Logging helps developers to track some events in the application as and when they occur. The Logging API allows all the python modules to participate in Logging, meaning custom messages can be integrated with the third-party modules. The Logging functionality has made error identification and resolving those errors faster. It is similar to a simple file-writer as it records down the track of the events into the console or as text format into files called log files with .log extension. The features of Logging are:

  • Multi-threading execution is possible
  • Categorize messages via different log levels
  • Flexible and configurable
  • Structured Information

Classes of Logging

1. Logger

The entry point of the logging system when Logging is called, and the events are recorded for processing. A message created by the Loggers called the log records are sent to one or more handlers that route the message to the final destination like console, file, or a network socket.

A logger is configured to have a log level that describes the severity of the messages that need to be handled by the logger. Generally, a python module consists of a single logger, but multiple loggers can be defined in a single module, or a logger can be used across multiple modules. It should always be initiated through module-level function logging.getLogger(name).

Python defines the following log levels:

LevelDescriptionWhen is it Used?Severity
DEBUGSystem information and everything is working fineFor Debugging Purposes10
INFOGeneral System InformationConfirm if things working as expected20
WARNINGInformation that describes the minor problem that has occurredIndicate some problem in near future30
ERRORInformation that describes the major problem that has occurredSome function not performing due to serious issues40
CRITICALInformation that describes the critical problem that has occurredThe program may stop running, indicating a serious issue50

The default log level is WARNING which will track events at this level and above unless the logging package is configured to desired. Django uses one of the following loggers:

Logger

Description

Context

 

django

Parent logger for messages

 

django.request

Log message related to the handling of requests.

 

5XX response raised as ERROR message

4XX response raised as WARNING message

status_code: HTTP response code associated with the request

request: Request object generating the message

 

 

django.server

Log message related to the handling of requests received by the server by runserver command.

 

5XX response raised as ERROR message

4XX response raised as WARNING message

Rest logged as INFO

 

django.template

Log message related to rendering templates.

The missing context is logged as DEBUG message

 

django.security.*

(DisallowedHost, csrf)

Log message on any SuspiciousOperation and other security issues.

Most occurance logged as WARNING.

SuspiciousOperation reaching WSGI handler logged as ERROR

 

django.db.backends.schema

Logs of SQL queries executed during schema changes by the migration framework.

sql: SQL statement that was executed

 

params: parameters used in SQL call

 

 

django.db.backends

Message related to the interaction of code with the database.

Application-level SQL statement executed by request logged at DEBUG level

duration: time to execute SQL statement

alias: alias of a database used in SQL call

 

There are several logger objects offered:

Logger ObjectDescription
Logger.debug(msg,*args, **kwargs)Logs message with level DEBUG
Logger.info(msg,*args, **kwargs)Logs message with level INFO
Logger.warning(msg,*args, **kwargs)Logs message with level WARNING
Logger.error(msg,*args, **kwargs)Logs message with level ERROR
Logger.critical(msg,*args, **kwargs)Logs message with level CRITICAL
Logger.log(level, msg,*args, **kwargs)Logs message with integer level LEVEL
Logger.exception(msg,*args, **kwargs)Exception info is added to the logging message
Logger.addFilter(filter), Logger.removeFilter(filter)Add or Remove specified filter to/from this logger
Logger.addHandler(hdlr), Logger.removeHandler(hdlr)Add or Remove specified handler to/from this logger
Logger.hasHandlers()Check if any loggers are configured to this logger.
Logger.setLevel(level)Set threshold for this logger to level. Logging messages with less severity will be ignored.

2. Handler

Handlers contain the information that determines what will happen to the log records in the loggers.  It has information about the location, the type of filter, and the formatter to be applied to the log record. Multiple loggers can use it. The default logging behavior is writing messages to the screen, a file, or a network socket. The various other handlers are provided by the logging module. The different forms of notification can be provided depending on the importance of the message, as a single logger can have multiple handlers.

3. Filters

The filters, as the name suggests, filter the messages. A filter is used to provide additional control over which log records are passed from logger to handler.

4. Formatters

A log record needs to be rendered as text. Formatters describe the exact format of that text. Handlers cannot send the information as it's a Python data type it needs to be converted.

Logging in Django:

Once you have configured your loggers, handlers, filters, and formatters, you need to place logging calls into your code. Using the logging framework works like this:

# import the logging library
import logging




# Get an instance of a logger
logger = logging.getLogger(__name__)




def my_view(request, arg1, arg):
    ...
    if bad_mojo:
        # Log an error message
        logger.error('Something went wrong!')

The logger instance contains an entry method for each of the default log levels:

  • logger.debug()
  • logger.info()
  • logger.warning()
  • logger.error()
  • logger.critical()

There are two other logging calls available:

  • logger.log(): Manually emits a logging message with a specific log level.
  • logger.exception(): Creates an ERROR level logging message wrapping the current exception stack frame.

Configuring Logging

Django provides Logging by using the logging module of Python. The logging module can be easily configured. For including Logging in Django, we need to configure its settings. Since Django works with different modules, we use the dictConfig method." dictConfig "is Django's default behavior.

The code for Logging is written below.

LOGGING = {
    'version': 1,
    # Version of logging
    'disable_existing_loggers': False,
    #disable logging 
    # Handlers 
    'handlers': {
        'file': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',
            'filename': 'django-debug.log',
        },
        'console': {
            'class': 'logging.StreamHandler',
        },
    },
    # Loggers 
    'loggers': {
        'django': {
            'handlers': ['file', 'console'],
            'level': 'DEBUG',
            'propagate': True,
            'level': os.getenv('DJANGO_LOG_LEVEL', 'DEBUG')
        },
    },
}

We get a built-in variable LOGGING from Django. Its logging default values come from this dictionary. Since we are configuring settings using a dictionary, it's called the dictConfig method.

There are some important keys inside the LOGGING dictionary.

  1. version
  2. disable_existing_loggers
  3. handlers
  4. loggers

Custom Logging Configuration

If you don't want to use Python's dictConfig format to configure your logger, you can specify your own configuration scheme. The LOGGING_CONFIG setting defines the callable that will be used to configure Django's loggers. By default, it points at Python's Logging.config.dictConfig() function. However, if you want to use a different configuration process, you can use any other callable that takes a single argument. The contents of LOGGING will be provided as the value of that argument when Logging is configured.


Related Topics

Django Pagination

Django is an open-source free to use python based web framework. Django provides many inbuilt tools for web developers to provide rapid web development. One of these tools is pagination....

4 minutes read.

Django Stack

Django is a popular Python web application framework that adheres to the "batteries-included" tenet. Batteries-guiding included idea is that common functionality for creating web applications should be provided with the...

5 minutes read.

Django Filters

Django is a high-level python based web framework. Django is mostly used as a backend engine for websites. Nowadays, every website communicates to a database to fetch all the information...

3 minutes read.

Django Project

Django Project Let's start to use it. Each web app you want to build in Django is called a project; and a project is a collection of apps. A software is a series of...

3 minutes read.

Creating View in Django

Creating View in Django    Django views are a key component of the frameworkbased applications.We are using Python function or class at their simplest, which takes up a web query and returns a web response.Views are used to get objects from the server, change objects if needed, render types, return HTML, and much more. Class Based View and Function based Django has two types of views: views based on functions (FBVs) and views based on class (CBVs).Initially, Django began with only FBVs, but then introduced CBVs as a way to model features so we didn't have to write boilerplate (i.e. the same software) code again and again.CBVs would be a way to increase efficiency, so you don't have to write as much code as you can. CBVs are classes of Python at their heart. Django ships with a range of "model" CBVs with pre-configured features that can be reused and often expanded.Then helpful names are given to these classes that explain what kind of functionality they provide.These are often referred to as "universal views" because they provide solutions to specific needs. The classes have a journal View function, or "view" for short, is simply a Python program that uses a web query and returns a web response. This response may include the HTML content of a web page, or a redirect, or a 404 error, or an XML file, or an image, etc. Example: You use a view to create a web page, remember that you need to connect a view to a URL to see it as a web page. Simple View We will create a simple view in myapplication to say “Welocome to django application” See the following view – from django.http import HttpResponse ...

4 minutes read.

Django ORM

Django is a Python web framework that enables easy and quick web development. Django has a handy and powerful feature called Django Object Relational Mapper, also known as Django ORM,...

3 minutes read.

How to create an app in Django

Create an application in Django Django is famous for its unique and fully managed application structure. For each functionality, an application can be created as a completely independent module. This article will take...

2 minutes read.

Django CSRF

Django is a high-level python based web framework. Django is open source and free to use for all. Django provides many features to web developers like scalability, security, rapid development,...

4 minutes read.

How to Implement Multiple User Types with Django

Django Framework: Django is a framework that can make web applications more accessible and faster. Django is an open-source collection of python libraries, and Django can be used for both the...

3 minutes read.

Django Logging

Every application developed has some bugs and errors. It is the responsibility of the developers to maintain the application by making it bug-free and error-free. To debug the application code...

5 minutes read.

Uses of Django

Django is a free and open-source Python-based web framework, which offers great functionalities to its users. Django was developed in 2003, and the final version was released in 2008 by...

3 minutes read.

What is Django?

What is Django? Django is a free and open-source web application framework, written in Python. A web framework is a set of components that help you develop an easier and faster website. When we...

3 minutes read.

Django vs Laravel

Django Overview Django is a Python Programming Open Source framework for web development. It was released in 2008 and was developed by two web developers named Adrian Holovaty and Simon Willison....

4 minutes read.

Django Celery

Django is a python-based web framework. It is an open-source and free-to-use framework. Django uses the Model View Template architecture. Django provides many functionalities to web developers. One of these...

3 minutes read.

Django Template System

Django Template System Django allows Python and HTML to be divided, the Python goes into views, and HTML goes into templates. Django relies on the render feature and the language of the Django Model...

5 minutes read.

Best Django courses

In this tutorial, we will see various courses in python Django available on the internet. Django has become popular these days, so courses on it are also increasing day by...

7 minutes read.

Django Channels

Channels is a project that uses Django and adds some functionalities beyond the traditional HTTP by using WSGI and ASGI.  We can use both synchronous and asynchronous requests with ASGI...

5 minutes read.

How to connect Django with Mysql

Any web application contains some data that comes from the input of the end-users. Thus, the database is required to handle those data. Several databases could be used to connect...

3 minutes read.

Django Model Fields

In Django, if we want to store the information about anything, we use Models. It contains various fields about the data. Generally, each model represents a database where all the...

4 minutes read.

Django REST Framework

Django REST Framework (DRF)is an open-source, powerful, and flexible toolkit used to build RESTful web APIs. It is built upon the Django framework. DRF increases the development speed. Django and...

4 minutes read.