How to Use Logging in 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:
- Loggers
- Handlers
- Filters
- 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 ProjectIn 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 exampleStep 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 coreStep 3: Update setting.py
Then update INSTALLED_APPS within our settings.py file to notify Django about the app.
settings.py.... INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'core', ] LOGGING = { '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/views.py file and add.
core/views.pyfrom 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(datetime.datetime.now())+' hours!') return HttpResponse("<h1>Welcome to Tuts-Station.com!)</h1>")Step 5: Creating URLs
In this section, we need a urls.py file within the core app however Django doesn't create one for us with the startapp command. Create core/urls.py with your text editor and paste below code.
core/urls.pyfrom 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.
example/urls.pyfrom django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), 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 manage.py runserver
Next, go to the http://localhost:8000/ address with a web browser.
OutputYou can see the below output warning.log file:
warning.logCurrent 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....