A robust and customizable logging system for Django applications, designed to log HTTP requests in structured JSON format. This package separates successful (status codes < 400) and failed (status codes >= 400) requests into distinct log files (info.log and errors.log), facilitating efficient monitoring and analysis of application behavior.
- Comprehensive Request Logging: Captures method, path, status code, client IP, and User-Agent for every request.
- Structured JSON Logs: Easily parse and analyze with tools like Kibana or Graylog.
- Log Separation by Status Code:
- ✅
logs/info.log: Logs successful requests (status < 400). - ❌
logs/errors.log: Logs failed requests (status >= 400).
- ✅
- Custom Error Messages:
- 404 → "Not Found"
- 401 → "Unauthorized"
- Log Rotation:
- Max file size: 15MB
- Backups: 10 for info, 5 for error
- Lightweight: File-based logging with no email alerts.
-
Install required package:
pip install python-json-logger
-
Integrate with Django Project:
-
Copy the
logging_config.pyfile to your Django project directory (e.g.,your_project/config/). -
Update your Django
settings.pyto include the logging configuration:from pathlib import Path from .config.logging_config import LOGGING BASE_DIR = Path(__file__).resolve().parent.parent
-
Add the
RequestLoggingMiddlewareto theMIDDLEWAREsetting insettings.py:MIDDLEWARE = [ # ... other middleware 'your_project.config.logging_config.RequestLoggingMiddleware', ]
-
The RequestLoggingMiddleware automatically logs all HTTP requests to your Django application:
- Successful Requests (status codes < 400, e.g., 200, 201) are logged in
logs/info.log. - Failed Requests (status codes >= 400, e.g., 401, 404, 500) are logged in
logs/errors.log.
info.log Example (Successful Request):
{
"levelname": "INFO",
"asctime": "2025-04-30 06:52:09,533",
"name": "project.requests.info",
"message": "HTTP GET request to /api/v1/bmc/communication-channel/",
"pathname": "...",
"lineno": 128,
"request_method": "GET",
"request_url": "/api/v1/bmc/communication-channel/",
"status_code": 200,
"remote_addr": "127.0.0.1",
"user_agent": "PostmanRuntime/7.43.3"
}errors.log Example (Failed Request):
{
"levelname": "ERROR",
"asctime": "2025-04-30 06:52:13,823",
"name": "project.requests.error",
"message": "Unauthorized: /api/v1/bmc/communication-channel/",
"pathname": "...",
"lineno": 126,
"request_method": "GET",
"request_url": "/api/v1/bmc/communication-channel/",
"status_code": 401,
"remote_addr": "127.0.0.1",
"user_agent": "PostmanRuntime/7.43.3"
}
HTTP Request
│
▼
RequestLoggingMiddleware
│
├── Success Logs
│ ▼
│ info.log
│
└── Error Logs
▼
errors.log
Django Models
│
▼
BaseModel
│
▼
AuditLogModelBase
│
▼
auditlog.register()
│
▼
Audit History
All application models should inherit from BaseModel.
class Customer(BaseModel):
full_name = models.CharField(max_length=255)Provided fields:
| Field | Description |
|---|---|
| id | UUID primary key |
| created_by | User who created the record |
| created_at | Creation timestamp |
logs/
├── django.log
├── debug.log
├── errors.log
├── info.log
├── payments.log
├── notifications.log
└── any_custom_logger.log
Example settings:
LOG_MAX_BYTES = 15 * 1024 * 1024
LOG_BACKUP_COUNT = 10Track all data modifications across business entities.
Maintain audit trails for transactions and account changes.
Record user activity and sensitive data modifications.
Monitor API usage and investigate incidents quickly.
Support auditing requirements for internal and external reviews.
Ensure your project directory includes a logs/ folder to store the log files. The middleware will create info.log and errors.log automatically. A suggested structure is:
your_project/
├── config/
│ ├── settings.py
│ └── logging_config.py
├── app_name
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── serilizers.py
│ ├── views.py
│ └── urls.py
└── ...
- Ensure the
logs/directory has appropriate write permissions for the application to create and write to log files. - The logging configuration is optimized for JSON output, making it compatible with log aggregation tools.
- For custom loggers, refer to
logging_config.pyto extend logging for specific application modules.
Contributions are welcome! Please submit issues or pull requests to the GitHub repository. Ensure any changes include tests and adhere to the project's coding standards.
This project is licensed under the MIT License. See the LICENSE file for details.