DEV Community

Cover image for Logging In Django
Max Ong Zong Bao
Max Ong Zong Bao

Posted on • Originally published at maxongzb.com

Logging In Django

Introduction

undraw annotation 7das

Recently I wrote an article called The Skills to get a job as a Django Developer about the necessary skillsets to learn to get a job as a Django developer.

A reader pointed out that we should learn to log data as part of the necessary skills to get a job as a developer. I think it could help me in learning and expand my knowledge about Python's logging library and apply it to Django. So let's get started.

Logging Basics

undraw observations mejb

Alright for context I shall explain the purpose of logging. We log data is to help you to solve problems whenever your application crashes. This allows you to know what had happened to your web server so that it could provide you with a way to diagnose your problem. Which may not be replicated on a development or staging server.

Sure this does not help you to diagnose what is wrong. But it's a starting point to help you to fix your problems that were encountered when your production server crashes.

Getting Started

undraw dev focus b9xo

In Django, the logging library is bundled with the standard library. It is easy to import and to start using it:

import logging

logger = logging.getLogger(__name__)
Enter fullscreen mode Exit fullscreen mode

The variable logger is the instance of the logging library. The __name__ as a parameter will evaluate to the dotted Python path of the module. This means __name__ is the name of your logger.

This helps you to know where is the log being called to zone in on the problem for your module:

info = 'This is an info message.'

# The "warning" log level of logger
logger.warning("this is a warning message")

 # Using an f string literal to use the "info" variable
logger.info(f'Info: {info}')

# Logging the exception that is caught in your code 
try:
    function_that_might_raise_index_error()
except IndexError:
    # equivalent to logger.error(msg, exc_info=True)
    logger.exception("Something bad happened")
Enter fullscreen mode Exit fullscreen mode

Here are the various log level used in the logger library in the order of severity from least to the most severe log level:

  • debug - This is not needed for regular operations but useful for local development.
  • info - Provides operation information of your server in development, staging & production.
  • warning - This might be a starting point for you to look at but is not urgent.
  • error - This requires immediate attention to solve it in production
  • critical - It could be used to identify the common problems related to the production server is crashing like the database has failed or your AWS service is down.

Where Should I Log?

undraw quiz nlyh

The location of your log file should be in a centralised location. Within a server, where ease of storage and retrieval of the logs so that analysis could be performed.

It could be as simple as just having a logs folder in your project in the system or within a database server.

Configuring Logger

undraw abstract x68e

During my research, I found that to configure the logger in Django. We disable the sending error emails and setting up sentry which makes sense to do that in Django.

Plus the Django logging gist by Peter Baumgartner from Linconloop is easy to understand compared to many of the articles on the subject. So I would recommend taking a look if you are into logging for your Django program.

Conclusion

undraw Ride till I can no more 44wq

I hope it might be useful to offer you a glimpse of logging for both Python & Django.

I think it will be a good starting point for you. To look at either Django Logging, The Right Way for a direct application of logging in Django and RealPython's Logging in Python to provide you with a general overview of what logging is in Python.

If you like this article sign up for my Adventurer's Newsletter for a weekly update in the area of Python, Startup and Web Development.

You can also follow me to get the latest update of my article on Dev

The original post was on Logging In Django - Reading Time: 3 Mins and cover image by Photo by Ian Battaglia on Unsplash

Reference

Top comments (0)