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 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.

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 DRF

Django Rest Framework, which is also referred to as DRF, is a very powerful application to build RESTful APIs in a Django application. DRF uses the Django framework. Similar to...

3 minutes read.

Django and React

This article will discuss the overview of Django and React, their advantages, applications, and most importantly, how to implement react in Django. What is Django? Django is a free and open-source web...

6 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 Frontend

Django 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 front...

3 minutes read.

Django Framework

Django is a popular high-level open-source web development Python framework based on follows Model View Template (MVT) architecture that allows rapid development. It. It provides common functionality like Admin Interface,...

3 minutes read.

Django All Auth

For any web application, user registration and authentication are the essential parts. In Django, there are several applications like django-registration-redux to perform these tasks, but the major drawback is that...

6 minutes read.

Django Environment Setup

Django is a free and open-source web framework, which uses Python as its programming language. It is based on Model View Template (MVT) architecture. A framework is a collection of...

4 minutes read.

Django – Sending E-mails

Django – Sending E-mails Django comes with a light engine that is ready and simple to use to send emails. Similar to Python, you just need to import a smtplib. You just need to import django.core.mail in Django. Edit the project settings.py file to start sending emails and set  all the following options: EMAIL_HOST – server with smtp.EMAIL_HOST_USER – smtp server login key.EMAIL_HOST_PASSWORD ? Password credential for the smtp server.EMAIL_PORT – server port smtp.EMAIL_USE_TLS or _SSL – if it is secure connection then set True. Simple E-mail Sending To submit a simple e-mail, let's create it a "simpleEmail" view. from django.core.mail import...

3 minutes read.

Django Admin

To manage the content of the website, e.g., adding and deleting posts, web developers need an admin portal on the website. The admin portal allows trusted users, also knows as...

3 minutes read.

Django Create App

Django is a Python-based web framework that is based on the Model View Template architecture. In this post, we will go through the installation and setup for our first Django...

4 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.

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.

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 CMS

Django is one of the most popular open-source Python web development framework. Developers use Django to build a website from scratch. Like WordPress, Django CMS is also a Content Management...

9 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 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.

Django Create Project

Django is an open-source free to use web framework which is based on Python. Django uses Model-View-Template as its architecture. The final version of Django was released in 2008. Django...

4 minutes read.

Django MVT

 MVT(Model View Template) MVT means Model View Template is a software design pattern The View is used to implement the business logic and to communicate with a model for data transmission and to...

2 minutes read.