How to Configure Logging Feature to print variable in Django Application

Posted on

Introduction

This article is showing about how to configure a logger in a Django-based application. This is important to print out any information which is very important for debugging the Django-based application when it is running. Actually, the settings is very simple. The following is the steps for configuring the logging feature in Django-based application :

  1. Access any ‘views.py’ file available in any root application folder available.

  2. Import the logging module by declaring the following line in the ‘views.py’ :

    import logging
  3. Define the basic configuration for the logging utility as in the following configuration :

    logging.basicConfig(Level=logging.WARNING)

    The first important configuration line to be able to use logger functionality is to define the basicConfig() function. It is a function which is very important to configure the root logger. It does basic configuration for the logging system by creating a stream handler with a default formatter. Defining it with a filename as part of the parameter of that function, it will set the file as the target write the log messages. The debug(), info(), warning(), error() and critical() call basicConfig() automatically if no handlers are defined for the root logger. Since there is no handler in this logging configuration, it is very important to define the basicConfig() function as in the above configuration line.

  4. Define the logging function for django as follows :

    logger = logging.getLogger('sysapp')

    Actually, the above declaration which is calling the ‘getLogger’ function is a way to create an instance of a logger. The instance itself will be stored in the variable with the name of ‘logger’. In this context, the naming of logger utility exist as the parameter of the ‘getLogger’ function. As an example, it is using the name of the application which is ‘sysapp’. For further information about the logging name, just check it in the documentation in this link.

  5. Another important information is setting the level of the logger. It is available by calling the function setLevel. The following is an example to set the level of the logger :

    logger.setLevel(logging.DEBUG)

    In order to find out about how many types of the level for the logger is available, once again read it in the documentation which exist in this link. In the documentation link, there are several level types including DEBUG, INFO, WARNING, ERROR and CRITICAL. It is actually describing the severity of the messages that the logger will handle.

Testing the Logging Function

After successfully defining the minimum requirement for performing simple logging task just to print a variable, the following is the step to test the logging function. By trying to print the content of a variable using the logger utility, it will demonstrate a simple task which is available in the logging utility exist. The following is an example for using the logger to print a variable :

  1. Adding the variable for further logging in the function :

    data = "This is the content of the data variable"
  2. Adding the logging script to print the variable as follows :

    logger.info(data)
  3. So, overall, the script for logging the variable with the name of data exist as follows :

        
    def main(request):
            data = "This is the content of the data variable"
            logger.info(data)
            template = loader.get_template('templates/sysapp/main.html')
    
  4. Last but not least, just run the application once more and the following output will appear :

    (env) C:\programming\python\django\myproject>python manage.py runserver 8001
    Watching for file changes with StatReloader
    Performing system checks...
    System check identified no issues (0 silenced).
    October 27, 2021 - 19:56:37
    Django version 3.2.6, using settings 'myproject.settings'
    Starting development server at http://127.0.0.1:8001/
    Quit the server with CTRL-BREAK.
    INFO:sysapp:This is the content of the data variable
    [27/Oct/2021 19:56:39] "GET /sysapp/ HTTP/1.1" 200 97
    

One thought on “How to Configure Logging Feature to print variable in Django Application

Leave a Reply