How to Use Logging in Django?

Published On: 11/01/2023 | Category: Django

Hi Guys,

Today, I will let you know example of how to use logging in django. This tutorial will give you simple example of how to implement logging in django. let’s discuss about how to use logger in django. I explained simply step by step django logging tutorial.

In Python will often use print() in their code as a quick and convenient debugging tool. Using the logging framework is only a little more effort than that, but it’s much more elegant and flexible.

In Python logging configuration consists of four parts if you want to need detailed about logger go to Django Logging:

  1. Loggers
  2. Handlers
  3. Filters
  4. Formatters

Here i will give you we will help you to give example of how to use logging in django. So let's see the bellow example:

Step 1: Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2: Create an App

Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.

django-admin startapp core
Step 3: Update

Then update INSTALLED_APPS within our file to notify Django about the app.


    'version': 1,
    # The version number of our log
    'disable_existing_loggers': False,
    # django uses some of its own loggers for internal operations. In case you want to disable them just replace the False above with true.
    # A handler for WARNING. It is basically writing the WARNING messages into a file called WARNING.log
    'handlers': {
        'file': {
            'level': 'WARNING',
            'class': 'logging.FileHandler',
            'filename': BASE_DIR / 'warning.log',
    # A logger for WARNING which has a handler called 'file'. A logger can have multiple handler
    'loggers': {
       # notice the blank '', Usually you would put built in loggers like django or root here based on your needs
        '': {
            'handlers': ['file'], #notice how file variable is called in handler which has been defined above
            'level': 'WARNING',
            'propagate': True,
Step 4: Creating the Views

In this step, we need to configure views. open the core/ file and add.

from django.shortcuts import render
from django.http import HttpResponse
import datetime
# import the logging library
import logging
# Get an instance of a logger
logger = logging.getLogger(__name__)

def hello_reader(request):
    logger.warning('Current Date and Time is '+str(' hours!')
    return HttpResponse("<h1>Welcome to!)</h1>")
Step 5: Creating URLs

In this section, we need a file within the core app however Django doesn't create one for us with the startapp command. Create core/ with your text editor and paste below code.

from django.urls import path
from . import views

urlpatterns = [
    path('',views.hello_reader, name="hello_reader"),

Next, we require to add a URL path for our core app which can be done by importing include and setting a path for it.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('core.urls')),
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the http://localhost:8000/ address with a web browser.


You can see the below output warning.log file:

Current Date and Time is 2023-01-11 05:01:26.951101 hours!
Current Date and Time is 2023-01-11 05:01:36.390285 hours!

I Hope It will help you....