{"id":155,"date":"2021-12-02T00:00:22","date_gmt":"2021-12-02T00:00:22","guid":{"rendered":"https:\/\/zerotobyte.com\/?p=155"},"modified":"2021-12-20T15:05:13","modified_gmt":"2021-12-20T15:05:13","slug":"introduction-to-django-logging-with-best-practices","status":"publish","type":"post","link":"https:\/\/zerotobyte.com\/introduction-to-django-logging-with-best-practices\/","title":{"rendered":"Introduction to Django logging with Best Practices"},"content":{"rendered":"\n
Django logging<\/strong> is one of few basic Django concepts that developers usually neglect but is important to master.<\/p>\n\n\n\n So what exactly is logging? Firstly, we need to understand the concept of logging, its advantages, disadvantages, and why we use it.<\/p>\n\n\n\n A good understanding, writing, and reading of logs can in the future make debugging easier and, among other things, prevent problems that may occur. While it may not seem like it, the key to creating large stable Django applications lies in logging.<\/strong><\/p>\n\n\n\n Django logging module consists of 4 parts:<\/p>\n\n\n\n The simplest example of using a logging module is shown in the following example:<\/p>\n\n\n\n Example 1.<\/strong><\/p>\n\n\n\n And that’s it, the logger instance is defined and ready to use! No need to install additional libraries or modules.<\/strong> Plug and play!<\/p>\n\n\n\n Official Django documentation on logging<\/a>.<\/p>\n\n\n\n Loggers have certain log levels<\/strong> that describe events on our backend, let\u2019s dive into it!<\/p>\n\n\n\n
Logging in Django is the process of storing certain records in some form of container (file, database, memory,\u2026) which then helps us in certain stages of development.<\/strong><\/p>\n\n\n\nWhat is Django logging?<\/h2>\n\n\n\n
import logging\n\n# logger instance with name of module where it\u2019s used (good practice)\nlogger = logging.getLogger(__name__)\n\nlogger.error(\u201cDummy text\u201d)\n<\/pre>\n\n\n\n
Loggers, Log levels, and when to use one?<\/h2>\n\n\n\n