Logging In Django
Tags: python, webdev, django, beginners
Published: 2020-01-12 08:31:17

# Introduction
![undraw annotation 7das][1]

Recently I wrote an article called [The Skills to get a job as a Django Developer][2] 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][3] library and apply it to Django. So let's get started. 

# Logging Basics
![undraw observations mejb][4]

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][5]

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

```python
import logging

logger = logging.getLogger(__name__)
```

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:

```python
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")
```

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][6]

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][7]

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

Plus the Django logging [gist][10] by **Peter Baumgartner** from [Linconloop][11] 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][12]

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][9] for a direct application of logging in Django and RealPython's [Logging in Python][13] 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](https://maxongzb.activehosted.com/f/1) 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](https://www.maxongzb.com/logging-in-django-reading-time-3-mins/) and cover image by [Photo by Ian Battaglia on Unsplash](https://unsplash.com/photos/9drS5E_Rguc)



# Reference
* [Sentory - Python][14]
* [Sentory - Django][15]
* [Django Documentation][16]
* [Python Logging][17] 
* [Django Logging The Right Way][9]
* [Python and Django Logging in Plain English][18]
* [Disabling Error Email Django][8]
* [Undraw][19]


[1]: //images.ctfassets.net/ly2f59p4unnn/4bSb6X3BwfIamRdGEPDGsu/0344c22888b8aa01bcdd5eb03e1f27d1/undraw_annotation_7das.png
[2]: https://www.maxongzb.com/the-skills-to-get-a-job-as-a-django-developer-reading-time-4-mins/
[3]: https://docs.python.org/3/library/logging.html
[4]: //images.ctfassets.net/ly2f59p4unnn/4J1NUIVEgyRmXkU5tl4R7h/9edaf1d2ad170ff1e354f8c8b6da590e/undraw_observations_mejb.png
[5]: //images.ctfassets.net/ly2f59p4unnn/44D3WN56heG3AgEt1iUb9r/5077ff7c027a6c742ea66582bf787270/undraw_dev_focus_b9xo.png
[6]: //images.ctfassets.net/ly2f59p4unnn/5IFlJuy6vIV6s1VgZ5R9Js/60d571ce710a098ca3825647c56fdc00/undraw_quiz_nlyh.png
[7]: //images.ctfassets.net/ly2f59p4unnn/4mSWhJWwV9dZ44SalgOQVJ/e1eca8b3da3645553e8cf067ab68e06a/undraw_abstract_x68e.png
[8]: https://lincolnloop.com/blog/disabling-error-emails-django/
[9]: https://lincolnloop.com/blog/django-logging-right-way/
[10]: https://gist.github.com/ipmb/0618f44dc5270f9a2be2826d0d933ed7
[11]: https://lincolnloop.com/
[12]: //images.ctfassets.net/ly2f59p4unnn/3VY8x6x83ZagNnZ38pVUnL/bbd52bae4dc035d299dd1ee447a1dce7/undraw_Ride_till_I_can_no_more_44wq.png
[13]: https://realpython.com/python-logging/#keep-calm-and-read-the-logs
[14]: https://docs.sentry.io/platforms/python/
[15]: https://sentry.io/for/django/
[16]: https://docs.djangoproject.com/en/3.0/topics/logging/
[17]: https://realpython.com/python-logging/
[18]: https://djangodeconstructed.com/2018/12/18/django-and-python-logging-in-plain-english/
[19]: https://undraw.co/illustrations