Log Django Applications like a Pro! — Part 2

SoftwareAlchemy
FAUN — Developer Community 🐾
3 min readMar 31, 2024

--

request_id in log statements

In the second part of this pro-level logging series, we are going to dive into the concept of a unique request identifier called a request_id attached to each log statement.

Let’s get straight to that.

What is request_id?

In web applications, the request_id is a unique identifier associated with each incoming HTTP request.

Why should we care?

Including the request_id in log statements is a common practice for several reasons:

  1. Traceability and Debugging: When troubleshooting issues in a web application, having a unique identifier for each request can greatly aid in tracing the flow of execution through various components of the system. By including the request_id in log statements, developers and administrators can easily correlate logs generated by different parts of the system for a single request, making it easier to diagnose and debug problems.
  2. Contextual Information: Logs are often generated asynchronously and may not always capture the exact context of a particular request. By including the request_id in log statements, each log entry is associated with the specific request it pertains to, providing valuable contextual information that can help in understanding the sequence of events leading up to a particular log entry.
  3. Auditing and Compliance: In certain industries or applications where auditing and compliance are critical, having a unique identifier for each request logged can be essential for tracking and ensuring accountability for all actions taken within the system.
  4. Performance Monitoring: Including the request_id in log statements can also facilitate performance monitoring and analysis. By aggregating logs based on request_id, administrators can analyze the performance characteristics of individual requests and identify potential bottlenecks or areas for optimization.

How to add request_id in Django Applications?

Adding request_id in logs for Django Applications is a simple 2-step process:

  1. Install the required package:
pip3 install django-log-request-id

2. Update your LOGGING settings in settings.py

LOGGING = {
"version": 1,
"disable_existing_loggers": True,
"filters": {"request_id": {"()": "log_request_id.filters.RequestIDFilter"}}, # This filter attaches a request_id to each log statement
"formatters": {
"standard": {"format": "%(asctime)s [%(levelname)s]- %(message)s"},
"json": {
"()": "core.utils.logging.StandardJSONLogFormatter",
},
},
"handlers": {
"console": {
"level": "INFO",
"class": "logging.StreamHandler",
"formatter": "json",
"filters": ["request_id"],
},
},
"loggers": {
"hb_logger": {
"handlers": ["console"],
"level": "INFO",
"propagate": True,
},
},
}

3. Done. You can go ahead and use the logger to log messages containing a unique request_id filter attached to each log statement.

logger.info("JSON Logging is really cool!")

Log output:

{"request_id": "7101193f71ef4063a6d7cb06ec3d2754", "name": "my_logger", "level": "INFO", "exc_info": null, "thread": 6174552064, "message": "JSON Logging is really cool!", "time": "2024-03-31T07:11:48.793425+00:00"}

NOTE: This article is a part of the series: Log Django Applications Like a Pro!

Checkout the Part 1 to learn about JSON Logging in case you have missed that one.

Follow us for more such content!

Stay Tuned!

Happy Coding!

👋 If you find this helpful, please click the clap 👏 button below a few times to show your support for the author 👇

🚀Join FAUN Developer Community & Get Similar Stories in your Inbox Each Week

--

--

Helping developers level up with insights on Java, Spring, Python, databases, and more. Follow along and let's build something great together!