Logging System
This section details the logging system implemented in the Django Starter Template, which is designed for high-performance production environments using 12-Factor App methodology.
Overview
The logging system is configured in conf/settings.py to stream all events to stdout (Console) in structured JSON format. This eliminates the need for managing local log files and integrating easily with modern container logging drivers (Docker, Kubernetes) and aggregators (Datadog, Splunk, CloudWatch).
Key Features
- JSON Format: All logs are formatted as JSON, enabling machine readability and easier parsing.
- Console Only: Adheres to "logs as event streams" philosophy. No files are written to disk.
- Unified Logger: Centralized logger strategy simplifies configuration and prevents "lost" logs.
- Request Tracing: A modern, async-safe
RequestIDMiddlewareassigns a uniquerequest_idto each request. This is stored inContextVars(immune to thread-bleeding issues) and injected into every log line. - Automatic Context: Every log entry automatically includes:
request_idclient_ipuser_id(if authenticated)pathresponse_time(for completed requests)
Log Levels & Categories
The system uses three primary logger categories:
- Root (
""): format catching all third-party libraries. - Framework (
"django"): Captures internal framework events (DB queries, generic errors). - Application (
"apps"): Captures your business logic logs.
How to Log in Your Code
Do not use print(). Use the standard Python logging module with the __name__ convention:
import logging
logger = logging.getLogger(__name__)
def my_view(request):
logger.info("Processing order %s", order_id)
try:
...
except ValueError as e:
logger.warning("Invalid input received: %s", e)
Note: Always use lazy interpolation (e.g., logger.info("Msg %s", var)), NOT f-strings, to improve performance.