Log Django Applications like a Pro! - Part 1
Your ultimate guide to logging in Django Applications
Modern web applications employ a myriad of logging methodologies. One of the most popular one is the JSON Formatted Logs.
What are JSON Formatted logs?
In JSON formatted logs, each log entry is represented as an object, with attributes such as timestamp, log level, message, and additional contextual information. This format allows for flexibility in capturing various types of data, including error messages, user actions, server responses, and more.
Why JSON Formatted Logs?
JSON-formatted web application logs provide a structured and standardized way to store and transmit log data generated by web applications. Instead of using plain text or other formats, JSON logs organize information into key-value pairs, making it easier for developers and systems to parse and analyze the data.
Enabling JSON Formatted Logs in a Python Django Application
Install the necessary packages:
pip3 install json_log_formatter
Write a custom JSON Log Formatter to adjust to the application’s logging needs:
from json_log_formatter import JSONFormatter
class StandardJSONLogFormatter(JSONFormatter):
def json_record(self, message, extra, record):
request = extra.pop("request", None)
if request:
# Add any other parameter
extra["IP_ADDRESS"] = request.META.get(
"HTTP_X_FORWARDED_FOR"
) # or other ways to get ip
additional_info = {
"name": record.name,
"level": record.levelname,
"file": record.filename,
"exc_info": record.exc_info,
"thread": record.thread,
}
extra = {**extra, **additional_info}
return super().json_record(message, extra, record)
Update Django Settings to include our newly setup JSON Log Formatter:
LOGGING = { "version": 1, "disable_existing_loggers": True, "formatters": { "standard": {"format": "%(asctime)s [%(levelname)s]- %(message)s"}, "json": { "()": "my.path.to.StandardJSONLogFormatter", }, }, "handlers": { "console": { "level": “INFO”, "class": "logging.StreamHandler", "formatter": "json" }, }, "loggers": { "hb_logger": { "handlers": ["console"], "level": “INFO”, "propagate": True, }, }, }
Define a logger:
import logging logger = logging.getLogger("my_logger")
Done. Use the logger defined above to log the messages in your application!
logger.info(“JSON Logging is really cool!”)
Thats’s all for part 1 of this series. Next up we will learn how to attach a unique request_id to each log statement.
Stay Tuned!
Happy Coding!