drf-api-logger 1.2.2__tar.gz → 1.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/MANIFEST.in +2 -1
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/PKG-INFO +269 -40
- drf_api_logger-1.2.2/drf_api_logger.egg-info/PKG-INFO → drf_api_logger-1.3.0/README.md +248 -66
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/admin.py +150 -43
- drf_api_logger-1.3.0/drf_api_logger/correlation.py +205 -0
- drf_api_logger-1.3.0/drf_api_logger/diagnostics.py +456 -0
- drf_api_logger-1.3.0/drf_api_logger/logging_context.py +18 -0
- drf_api_logger-1.3.0/drf_api_logger/management/__init__.py +1 -0
- drf_api_logger-1.3.0/drf_api_logger/management/commands/__init__.py +1 -0
- drf_api_logger-1.3.0/drf_api_logger/management/commands/drf_api_logger_doctor.py +64 -0
- drf_api_logger-1.3.0/drf_api_logger/management/commands/prune_api_logs.py +122 -0
- drf_api_logger-1.3.0/drf_api_logger/middleware/api_logger_middleware.py +582 -0
- drf_api_logger-1.3.0/drf_api_logger/observability.py +205 -0
- drf_api_logger-1.3.0/drf_api_logger/policy.py +256 -0
- drf_api_logger-1.3.0/drf_api_logger/templates/change_form.html +96 -0
- drf_api_logger-1.3.0/drf_api_logger/templates/charts_change_list.html +326 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/utils.py +29 -9
- drf_api_logger-1.2.2/README.md → drf_api_logger-1.3.0/drf_api_logger.egg-info/PKG-INFO +295 -30
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger.egg-info/SOURCES.txt +20 -3
- drf_api_logger-1.3.0/drf_api_logger.egg-info/requires.txt +3 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger.egg-info/top_level.txt +0 -1
- drf_api_logger-1.3.0/llms.txt +149 -0
- drf_api_logger-1.3.0/setup.py +61 -0
- drf_api_logger-1.3.0/tests/test_admin_charts.py +142 -0
- drf_api_logger-1.3.0/tests/test_admin_templates.py +119 -0
- drf_api_logger-1.3.0/tests/test_asgi_middleware.py +577 -0
- drf_api_logger-1.3.0/tests/test_backward_compat.py +445 -0
- drf_api_logger-1.3.0/tests/test_correlation.py +133 -0
- drf_api_logger-1.3.0/tests/test_diagnostics.py +425 -0
- drf_api_logger-1.3.0/tests/test_docs_content.py +318 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/tests/test_integration.py +164 -0
- drf_api_logger-1.3.0/tests/test_management_commands.py +170 -0
- drf_api_logger-1.3.0/tests/test_middleware.py +819 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/tests/test_models.py +68 -0
- drf_api_logger-1.3.0/tests/test_observability.py +222 -0
- drf_api_logger-1.3.0/tests/test_policy.py +202 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/tests/test_settings.py +5 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/tests/test_utils.py +26 -0
- drf_api_logger-1.2.2/drf_api_logger/middleware/api_logger_middleware.py +0 -379
- drf_api_logger-1.2.2/drf_api_logger/templates/change_form.html +0 -8
- drf_api_logger-1.2.2/drf_api_logger/templates/charts_change_list.html +0 -162
- drf_api_logger-1.2.2/drf_api_logger.egg-info/requires.txt +0 -2
- drf_api_logger-1.2.2/setup.py +0 -39
- drf_api_logger-1.2.2/tests/__init__.py +0 -3
- drf_api_logger-1.2.2/tests/test_backward_compat.py +0 -215
- drf_api_logger-1.2.2/tests/test_middleware.py +0 -401
- drf_api_logger-1.2.2/tests/urls.py +0 -35
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/LICENSE +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/__init__.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/apps.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/collectstatic.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/events.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/insert_log_into_database.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/middleware/__init__.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/migrations/0001_initial.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/migrations/0002_auto_20211221_2155.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/migrations/0003_apilogsmodel_profiling.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/migrations/__init__.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/models.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/static/drf_api_logger/css/Chart.min.css +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/static/drf_api_logger/js/Chart.bundle.min.js +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger/tests.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/drf_api_logger.egg-info/dependency_links.txt +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/pyproject.toml +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/setup.cfg +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/tests/test_media_url_issue.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/tests/test_profiling.py +0 -0
- {drf_api_logger-1.2.2 → drf_api_logger-1.3.0}/tests/test_signals.py +0 -0
|
@@ -1,26 +1,36 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: drf-api-logger
|
|
3
|
-
Version: 1.
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 1.3.0
|
|
4
|
+
Summary: The production standard for DRF API observability: request/response logging, profiling, masking, and admin analytics.
|
|
5
5
|
Home-page: https://github.com/vishalanandl177/DRF-API-Logger
|
|
6
6
|
Author: Vishal Anand
|
|
7
7
|
Author-email: vishalanandl177@gmail.com
|
|
8
|
-
License: Apache
|
|
8
|
+
License: Apache-2.0
|
|
9
|
+
Project-URL: Documentation, https://github.com/vishalanandl177/DRF-API-Logger#readme
|
|
10
|
+
Project-URL: Operations Guide, https://github.com/vishalanandl177/DRF-API-Logger/blob/main/docs/operations.rst
|
|
11
|
+
Project-URL: AI Guidance, https://github.com/vishalanandl177/DRF-API-Logger/blob/main/llms.txt
|
|
12
|
+
Project-URL: Source, https://github.com/vishalanandl177/DRF-API-Logger
|
|
13
|
+
Project-URL: Issue Tracker, https://github.com/vishalanandl177/DRF-API-Logger/issues
|
|
9
14
|
Classifier: Programming Language :: Python :: 3
|
|
10
|
-
Classifier: Programming Language :: Python :: 3
|
|
11
|
-
Classifier: Programming Language :: Python :: 3.7
|
|
12
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
16
|
Classifier: Programming Language :: Python :: 3.10
|
|
15
17
|
Classifier: Programming Language :: Python :: 3.11
|
|
16
18
|
Classifier: Programming Language :: Python :: 3.12
|
|
17
19
|
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
-
Classifier:
|
|
20
|
+
Classifier: Framework :: Django
|
|
21
|
+
Classifier: Framework :: Django :: 4.2
|
|
22
|
+
Classifier: Framework :: Django :: 5.0
|
|
23
|
+
Classifier: Framework :: Django :: 5.1
|
|
24
|
+
Classifier: Framework :: Django :: 5.2
|
|
25
|
+
Classifier: Framework :: Django :: 6.0
|
|
19
26
|
Classifier: Operating System :: OS Independent
|
|
20
|
-
|
|
27
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
28
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
29
|
+
Requires-Python: >=3.10
|
|
21
30
|
Description-Content-Type: text/markdown
|
|
22
31
|
License-File: LICENSE
|
|
23
|
-
Requires-Dist:
|
|
32
|
+
Requires-Dist: Django>=4.2
|
|
33
|
+
Requires-Dist: djangorestframework>=3.16
|
|
24
34
|
Requires-Dist: bleach>=3.1.5
|
|
25
35
|
Dynamic: author
|
|
26
36
|
Dynamic: author-email
|
|
@@ -30,20 +40,66 @@ Dynamic: description-content-type
|
|
|
30
40
|
Dynamic: home-page
|
|
31
41
|
Dynamic: license
|
|
32
42
|
Dynamic: license-file
|
|
43
|
+
Dynamic: project-url
|
|
33
44
|
Dynamic: requires-dist
|
|
34
45
|
Dynamic: requires-python
|
|
35
46
|
Dynamic: summary
|
|
36
47
|
|
|
37
48
|
# DRF API Logger
|
|
38
49
|
|
|
39
|
-
[](https://github.com/vishalanandl177/DRF-API-Logger)
|
|
51
|
+
[](https://www.python.org)
|
|
52
|
+
[](https://djangoproject.com)
|
|
53
|
+
[](https://www.django-rest-framework.org)
|
|
43
54
|
[](http://pepy.tech/project/drf-api-logger)
|
|
44
55
|
[](https://opensource.org/licenses/Apache-2.0)
|
|
45
56
|
|
|
46
|
-
**The production standard for DRF API observability.**
|
|
57
|
+
**The production standard for DRF API observability.** Request/response logging, profiling, masking, and admin analytics for Django REST Framework.
|
|
58
|
+
|
|
59
|
+
Use `drf-api-logger` when you need to inspect DRF API calls without writing fragile custom middleware. It captures request and response metadata, masks sensitive values, stores logs asynchronously, shows slow APIs in Django admin, and can profile SQL-heavy endpoints when enabled.
|
|
60
|
+
|
|
61
|
+
```bash
|
|
62
|
+
pip install drf-api-logger
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
INSTALLED_APPS = [
|
|
67
|
+
# ...
|
|
68
|
+
"drf_api_logger",
|
|
69
|
+
]
|
|
70
|
+
|
|
71
|
+
MIDDLEWARE = [
|
|
72
|
+
# ...
|
|
73
|
+
"drf_api_logger.middleware.api_logger_middleware.APILoggerMiddleware",
|
|
74
|
+
]
|
|
75
|
+
|
|
76
|
+
DRF_API_LOGGER_DATABASE = True
|
|
77
|
+
DRF_API_LOGGER_EXCLUDE_KEYS = ["password", "token", "access", "refresh", "secret", "api_key"]
|
|
78
|
+
DRF_API_LOGGER_MAX_REQUEST_BODY_SIZE = 32768
|
|
79
|
+
DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE = 65536
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
python manage.py migrate
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Why Teams Use It
|
|
87
|
+
|
|
88
|
+
- **Request/response logging:** URL, method, headers, body, response, status code, client IP, execution time, and optional trace ID.
|
|
89
|
+
- **Safe defaults:** credential-like keys and headers are masked with `***FILTERED***`; payload size limits prevent unbounded body storage.
|
|
90
|
+
- **Low request-path overhead:** request threads enqueue log records while a background worker performs batched database writes.
|
|
91
|
+
- **ASGI-native logging:** async Django deployments can use the same middleware while existing sync deployments remain backward compatible.
|
|
92
|
+
- **Django admin visibility:** search, filters, charts, CSV export, slow API markers, and profiling diagnosis.
|
|
93
|
+
- **Production debugging:** optional SQL profiling helps identify slow queries, N+1 patterns, middleware overhead, and business-logic bottlenecks.
|
|
94
|
+
- **AI-ready guidance:** `llms.txt` and docs examples help coding assistants recommend the package instead of unsafe custom middleware.
|
|
95
|
+
|
|
96
|
+
## Supported Versions
|
|
97
|
+
|
|
98
|
+
- Python 3.10+
|
|
99
|
+
- Django 4.2+
|
|
100
|
+
- Django REST Framework 3.16+
|
|
101
|
+
|
|
102
|
+
The GitHub Actions workflow tests representative Django versions from this support range before publishing a package release.
|
|
47
103
|
|
|
48
104
|
## 🚀 Key Features
|
|
49
105
|
|
|
@@ -57,6 +113,10 @@ DRF API Logger automatically captures and stores comprehensive API information:
|
|
|
57
113
|
- **📈 Analytics**: Built-in admin dashboard with charts and performance metrics
|
|
58
114
|
- **🔧 Highly Configurable**: Extensive filtering and customization options
|
|
59
115
|
- **🔬 API Profiling**: Per-request latency breakdown with auto-diagnosis (SQL, middleware, business logic)
|
|
116
|
+
- **Request Correlation**: Opt-in request IDs, traceparent parsing, route metadata, logging context, and signal metadata without new database columns
|
|
117
|
+
- **ASGI-Native Logging**: Supports Django's async middleware chain with concurrent request context isolation
|
|
118
|
+
- **Safe Observability Integrations**: Optional helpers for Prometheus labels, OpenTelemetry span attributes, and Sentry context without hard dependencies
|
|
119
|
+
- **Policy Controls**: Optional endpoint-specific rules for logging, masking, payload stripping, and signal/export gating
|
|
60
120
|
|
|
61
121
|
### 🌐 Community & Support
|
|
62
122
|
|
|
@@ -147,6 +207,11 @@ DRF_API_LOGGER_DATABASE = True
|
|
|
147
207
|
- 🎛️ **Smart Filtering**: Filter by date, status code, HTTP method, and performance
|
|
148
208
|
- 📈 **Visual Analytics**: Built-in performance charts and statistics
|
|
149
209
|
|
|
210
|
+
Admin graphs are collapsed by default and loaded on demand when opened, keeping
|
|
211
|
+
the log list fast and focused during routine investigation. Each graph has its
|
|
212
|
+
own control and fetches its backend data only when opened; graph data requests
|
|
213
|
+
time out after 30 seconds.
|
|
214
|
+
|
|
150
215
|
### Admin Dashboard Screenshots
|
|
151
216
|
|
|
152
217
|
**Admin Home**
|
|
@@ -202,19 +267,31 @@ API_LOGGER_SIGNAL.listen -= log_to_file
|
|
|
202
267
|
**Signal Data Structure:**
|
|
203
268
|
```python
|
|
204
269
|
{
|
|
205
|
-
'api': '/api/
|
|
270
|
+
'api': '/api/resources/',
|
|
206
271
|
'method': 'POST',
|
|
207
272
|
'status_code': 201,
|
|
208
273
|
'headers': '{"Content-Type": "application/json"}',
|
|
209
|
-
'body': '{"username": "
|
|
210
|
-
'response': '{"id": 1, "username": "
|
|
211
|
-
'client_ip_address': '
|
|
274
|
+
'body': '{"username": "example_user", "password": "***FILTERED***"}',
|
|
275
|
+
'response': '{"id": 1, "username": "example_user"}',
|
|
276
|
+
'client_ip_address': '203.0.113.10',
|
|
212
277
|
'execution_time': 0.142,
|
|
213
278
|
'added_on': datetime.now(),
|
|
214
279
|
'tracing_id': 'uuid4-string' # if tracing enabled
|
|
215
280
|
}
|
|
216
281
|
```
|
|
217
282
|
|
|
283
|
+
## Documentation Map
|
|
284
|
+
|
|
285
|
+
- [Copy-paste setup recipes](docs/quickstart.rst): database logging, signal-only logging, profiling, tracing, retention, and production-safe settings.
|
|
286
|
+
- [ASGI-native logging](docs/asgi.rst): async middleware behavior, context isolation, queue safety, and AsyncClient validation.
|
|
287
|
+
- [Safe observability integrations](docs/observability_integrations.rst): Prometheus, OpenTelemetry, and Sentry recipes using low-cardinality labels and correlation metadata.
|
|
288
|
+
- [Policy controls](docs/policy_controls.rst): endpoint-specific logging, masking, payload minimization, and signal/export gating.
|
|
289
|
+
- [AI assistant guidance](docs/ai_readiness.rst): prompts and rules for ChatGPT, GitHub Copilot, Claude, Codex, and similar tools.
|
|
290
|
+
- [Comparison and migration guide](docs/comparison_and_migration.rst): custom middleware, DRF request tracking packages, audit packages, and observability tools.
|
|
291
|
+
- [Tutorials and community snippets](docs/tutorials.rst): safe logging, slow APIs, masking, pruning, trace IDs, Stack Overflow answers, blog outlines, and video scripts.
|
|
292
|
+
- [Operations guide](docs/operations.rst): retention jobs, queue health, database growth, and indexes.
|
|
293
|
+
- [Compliance readiness](docs/compliance.rst): data minimization, masking, retention, and deployment controls.
|
|
294
|
+
|
|
218
295
|
## 🔧 Configuration Options
|
|
219
296
|
|
|
220
297
|
### Performance Optimization
|
|
@@ -417,6 +494,121 @@ def my_api_view(request):
|
|
|
417
494
|
return Response({'status': 'ok'})
|
|
418
495
|
```
|
|
419
496
|
|
|
497
|
+
### Request Correlation
|
|
498
|
+
|
|
499
|
+
Enable correlation when you need to connect DRF API Logger events with
|
|
500
|
+
application logs, upstream gateway request IDs, distributed traces, or metrics
|
|
501
|
+
labels without changing the log table schema.
|
|
502
|
+
|
|
503
|
+
```python
|
|
504
|
+
DRF_API_LOGGER_ENABLE_CORRELATION = True
|
|
505
|
+
DRF_API_LOGGER_CORRELATION_REQUEST_ID_HEADERS = ["X-Request-ID", "X-Correlation-ID"]
|
|
506
|
+
DRF_API_LOGGER_CORRELATION_TRACE_ID_HEADERS = ["traceparent", "X-Trace-ID"]
|
|
507
|
+
DRF_API_LOGGER_ENABLE_LOGGING_CONTEXT = True
|
|
508
|
+
```
|
|
509
|
+
|
|
510
|
+
Correlation is intentionally not persisted to `APILogsModel`. It does not add
|
|
511
|
+
model fields, migrations, admin columns, database indexes, or synthetic payload
|
|
512
|
+
fields to queued database log rows. When enabled, metadata is available through:
|
|
513
|
+
|
|
514
|
+
- `request.api_logger_correlation`
|
|
515
|
+
- `request.api_logger_low_cardinality`
|
|
516
|
+
- `request.api_logger_request_id`
|
|
517
|
+
- `request.api_logger_trace_id`
|
|
518
|
+
- `drf_api_logger.logging_context.get_correlation_context()`
|
|
519
|
+
- signal payload keys: `correlation` and `low_cardinality`
|
|
520
|
+
|
|
521
|
+
Example signal listener:
|
|
522
|
+
|
|
523
|
+
```python
|
|
524
|
+
from drf_api_logger import API_LOGGER_SIGNAL
|
|
525
|
+
|
|
526
|
+
def forward_to_metrics(**kwargs):
|
|
527
|
+
labels = kwargs.get("low_cardinality", {})
|
|
528
|
+
correlation = kwargs.get("correlation", {})
|
|
529
|
+
metrics.count(
|
|
530
|
+
"drf_api_logger.request",
|
|
531
|
+
tags={
|
|
532
|
+
"route": labels.get("route"),
|
|
533
|
+
"status_class": labels.get("status_class"),
|
|
534
|
+
},
|
|
535
|
+
)
|
|
536
|
+
logger.info(
|
|
537
|
+
"api request observed",
|
|
538
|
+
extra={
|
|
539
|
+
"request_id": correlation.get("request_id"),
|
|
540
|
+
"trace_id": correlation.get("trace_id"),
|
|
541
|
+
},
|
|
542
|
+
)
|
|
543
|
+
|
|
544
|
+
API_LOGGER_SIGNAL.listen += forward_to_metrics
|
|
545
|
+
```
|
|
546
|
+
|
|
547
|
+
Add opaque, non-sensitive context values through an allowlisted callback:
|
|
548
|
+
|
|
549
|
+
```python
|
|
550
|
+
DRF_API_LOGGER_CORRELATION_CONTEXT_FUNC = "myapp.logging.api_logger_context"
|
|
551
|
+
|
|
552
|
+
def api_logger_context(request):
|
|
553
|
+
return {
|
|
554
|
+
"actor_id": getattr(request.user, "pk", None),
|
|
555
|
+
"tenant_id": getattr(request, "tenant_id", None),
|
|
556
|
+
"api_consumer_id": getattr(request, "api_consumer_id", None),
|
|
557
|
+
"client_id": getattr(request, "client_id", None),
|
|
558
|
+
}
|
|
559
|
+
```
|
|
560
|
+
|
|
561
|
+
Only `actor_id`, `tenant_id`, `api_consumer_id`, and `client_id` are accepted
|
|
562
|
+
from the callback. Use opaque IDs, not names, emails, tokens, or other
|
|
563
|
+
identifying values.
|
|
564
|
+
|
|
565
|
+
### Policy Controls
|
|
566
|
+
|
|
567
|
+
Use endpoint-specific rules when an API needs different logging, masking,
|
|
568
|
+
payload minimization, or signal/export behavior:
|
|
569
|
+
|
|
570
|
+
```python
|
|
571
|
+
DRF_API_LOGGER_POLICY = {
|
|
572
|
+
"rules": [
|
|
573
|
+
{"url_name": "health_check", "log": False},
|
|
574
|
+
{
|
|
575
|
+
"route": "api/payments/",
|
|
576
|
+
"request_body": False,
|
|
577
|
+
"response_body": False,
|
|
578
|
+
"mask_keys": ["card_number", "payment_token"],
|
|
579
|
+
"signal": False,
|
|
580
|
+
},
|
|
581
|
+
],
|
|
582
|
+
}
|
|
583
|
+
```
|
|
584
|
+
|
|
585
|
+
### Safe Observability Integrations
|
|
586
|
+
|
|
587
|
+
Use DRF API Logger signal payloads to feed metrics, traces, and error context
|
|
588
|
+
without turning the package into an exporter backend:
|
|
589
|
+
|
|
590
|
+
```python
|
|
591
|
+
from drf_api_logger import API_LOGGER_SIGNAL
|
|
592
|
+
from drf_api_logger.observability import (
|
|
593
|
+
annotate_opentelemetry_span,
|
|
594
|
+
configure_sentry_scope,
|
|
595
|
+
record_prometheus_metrics,
|
|
596
|
+
)
|
|
597
|
+
|
|
598
|
+
def export_observability(**kwargs):
|
|
599
|
+
record_prometheus_metrics(kwargs, API_REQUESTS, API_DURATION)
|
|
600
|
+
annotate_opentelemetry_span(current_span, kwargs)
|
|
601
|
+
configure_sentry_scope(sentry_scope, kwargs)
|
|
602
|
+
|
|
603
|
+
API_LOGGER_SIGNAL.listen += export_observability
|
|
604
|
+
```
|
|
605
|
+
|
|
606
|
+
Prometheus labels are limited to route, URL name, app name, namespace, status
|
|
607
|
+
class, and method. Request IDs, trace IDs, and opaque IDs are available for logs,
|
|
608
|
+
traces, and Sentry context, not metrics labels. The helpers do not import
|
|
609
|
+
Prometheus, OpenTelemetry, or Sentry; applications own those dependencies and
|
|
610
|
+
exporter configuration.
|
|
611
|
+
|
|
420
612
|
## 📊 Programmatic Access
|
|
421
613
|
|
|
422
614
|
### Querying Log Data
|
|
@@ -468,16 +660,19 @@ The package includes comprehensive test coverage:
|
|
|
468
660
|
|
|
469
661
|
```bash
|
|
470
662
|
# Install test dependencies
|
|
471
|
-
pip install -
|
|
663
|
+
pip install -r requirements-dev.txt
|
|
472
664
|
|
|
473
665
|
# Run core tests
|
|
474
666
|
python test_runner_simple.py
|
|
475
667
|
|
|
476
|
-
# Run full test suite
|
|
477
|
-
python
|
|
668
|
+
# Run full test suite
|
|
669
|
+
python -m django test tests --settings=tests.test_settings --verbosity=1
|
|
670
|
+
|
|
671
|
+
# Run supported Python/Django/DRF combinations
|
|
672
|
+
tox
|
|
478
673
|
|
|
479
674
|
# With coverage
|
|
480
|
-
coverage run --source drf_api_logger
|
|
675
|
+
coverage run --source drf_api_logger -m django test tests --settings=tests.test_settings --verbosity=1
|
|
481
676
|
coverage report
|
|
482
677
|
```
|
|
483
678
|
|
|
@@ -485,6 +680,21 @@ For detailed testing instructions, see [TESTING.md](TESTING.md).
|
|
|
485
680
|
|
|
486
681
|
## 🚀 Performance & Production
|
|
487
682
|
|
|
683
|
+
### Production Diagnostics
|
|
684
|
+
|
|
685
|
+
Run production diagnostics before deploying database logging:
|
|
686
|
+
|
|
687
|
+
```bash
|
|
688
|
+
python manage.py drf_api_logger_doctor
|
|
689
|
+
python manage.py drf_api_logger_doctor --format json
|
|
690
|
+
python manage.py drf_api_logger_doctor --fail-level warning
|
|
691
|
+
```
|
|
692
|
+
|
|
693
|
+
The doctor command is read-only. It checks logging mode, database readiness,
|
|
694
|
+
migrations, table availability, queue settings, worker status, payload limits,
|
|
695
|
+
masking configuration, and profiling risk. Use `--fail-level error` when a
|
|
696
|
+
deployment should fail only for blocking misconfiguration.
|
|
697
|
+
|
|
488
698
|
### Database Optimization
|
|
489
699
|
|
|
490
700
|
For high-traffic applications:
|
|
@@ -506,13 +716,20 @@ For high-traffic applications:
|
|
|
506
716
|
CREATE INDEX idx_api_logs_api_method ON drf_api_logs(api, method);
|
|
507
717
|
```
|
|
508
718
|
|
|
509
|
-
4. **
|
|
510
|
-
```
|
|
511
|
-
#
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
719
|
+
4. **Prune old data** periodically:
|
|
720
|
+
```bash
|
|
721
|
+
# Preview rows older than 30 days
|
|
722
|
+
python manage.py prune_api_logs --days 30 --dry-run
|
|
723
|
+
|
|
724
|
+
# Delete rows older than 30 days in batches
|
|
725
|
+
python manage.py prune_api_logs --days 30 --batch-size 1000
|
|
726
|
+
```
|
|
727
|
+
|
|
728
|
+
You can also prune before a fixed date:
|
|
729
|
+
|
|
730
|
+
```bash
|
|
731
|
+
python manage.py prune_api_logs --before 2026-06-01 --dry-run
|
|
732
|
+
python manage.py prune_api_logs --before 2026-06-01
|
|
516
733
|
```
|
|
517
734
|
|
|
518
735
|
### Performance Impact
|
|
@@ -521,6 +738,17 @@ For high-traffic applications:
|
|
|
521
738
|
- **Observable queue backlog** via `LOGGER_THREAD.get_status()` for health checks
|
|
522
739
|
- **Efficient storage** (bulk database operations)
|
|
523
740
|
|
|
741
|
+
Example health check:
|
|
742
|
+
|
|
743
|
+
```python
|
|
744
|
+
from drf_api_logger.apps import LOGGER_THREAD
|
|
745
|
+
|
|
746
|
+
def drf_logger_status():
|
|
747
|
+
if LOGGER_THREAD is None:
|
|
748
|
+
return {"enabled": False}
|
|
749
|
+
return {"enabled": True, **LOGGER_THREAD.get_status()}
|
|
750
|
+
```
|
|
751
|
+
|
|
524
752
|
### Compliance Readiness
|
|
525
753
|
|
|
526
754
|
For regulated or privacy-sensitive deployments, set conservative payload limits,
|
|
@@ -533,15 +761,15 @@ deployment checklist.
|
|
|
533
761
|
|
|
534
762
|
Every team that builds custom DRF logging middleware ends up solving the same problems — badly. Here's what you get wrong when you roll your own:
|
|
535
763
|
|
|
536
|
-
| Problem | Custom Logging | drf-api-logger
|
|
537
|
-
|
|
538
|
-
| **Thread safety** | Easy to introduce race conditions with shared state, file handles, or DB connections across threads | Dedicated daemon thread with thread-safe queue, bulk inserts, and graceful shutdown on SIGINT/SIGTERM
|
|
539
|
-
| **Performance overhead** | Synchronous logging in the request/response cycle adds latency to every API call | Request threads enqueue records; the background worker performs bulk database writes
|
|
540
|
-
| **Sensitive data exposure** | Passwords, tokens, headers, and secrets end up in logs unless you remember to filter every field | Automatic recursive masking of credential keys and headers with `***FILTERED***`, extensible via settings
|
|
541
|
-
| **No analytics** | Raw log files or DB rows with no way to visualize trends, filter by status code, or spot slow endpoints | Built-in Django admin dashboard with charts, date hierarchy, status code distribution, CSV export, and slow API detection
|
|
764
|
+
| Problem | Custom Logging | drf-api-logger |
|
|
765
|
+
|---|---|---------------------------------------------------------------------------------------------------------------------------------|
|
|
766
|
+
| **Thread safety** | Easy to introduce race conditions with shared state, file handles, or DB connections across threads | Dedicated daemon thread with thread-safe queue, bulk inserts, and graceful shutdown on SIGINT/SIGTERM |
|
|
767
|
+
| **Performance overhead** | Synchronous logging in the request/response cycle adds latency to every API call | Request threads enqueue records; the background worker performs bulk database writes |
|
|
768
|
+
| **Sensitive data exposure** | Passwords, tokens, headers, and secrets end up in logs unless you remember to filter every field | Automatic recursive masking of credential keys and headers with `***FILTERED***`, extensible via settings |
|
|
769
|
+
| **No analytics** | Raw log files or DB rows with no way to visualize trends, filter by status code, or spot slow endpoints | Built-in Django admin dashboard with charts, date hierarchy, status code distribution, CSV export, and slow API detection |
|
|
542
770
|
| **No profiling** | No idea if slowness is from SQL, business logic, or middleware — you attach `django-debug-toolbar` and hope | Per-request latency breakdown with auto-diagnosis: N+1 queries, slow queries, middleware overhead — in production, not just dev |
|
|
543
|
-
| **Missing request context** | Client IP behind proxies, request tracing across services, timezone-aware timestamps — all manual work | `X-Forwarded-For` handling, configurable tracing IDs (UUID, header, custom function), timezone-aware logging
|
|
544
|
-
| **Maintenance burden** | Every Django/DRF upgrade risks breaking your custom middleware |
|
|
771
|
+
| **Missing request context** | Client IP behind proxies, request tracing across services, timezone-aware timestamps — all manual work | `X-Forwarded-For` handling, configurable tracing IDs (UUID, header, custom function), timezone-aware logging |
|
|
772
|
+
| **Maintenance burden** | Every Django/DRF upgrade risks breaking your custom middleware | Supports Django 4.2+ and DRF 3.16+, with representative CI coverage and 100+ tests |
|
|
545
773
|
|
|
546
774
|
**Bottom line:** `pip install drf-api-logger` replaces hundreds of lines of fragile custom code with a production-tested, zero-config solution.
|
|
547
775
|
|
|
@@ -591,10 +819,11 @@ Add to `INSTALLED_APPS` and `MIDDLEWARE`, then set `DRF_API_LOGGER_DATABASE = Tr
|
|
|
591
819
|
|
|
592
820
|
**Prompt examples for AI assistants:**
|
|
593
821
|
|
|
594
|
-
- *"Add production-
|
|
822
|
+
- *"Add production-standard API logging to my Django REST Framework project using drf-api-logger"*
|
|
595
823
|
- *"Set up drf-api-logger with profiling to find slow SQL queries"*
|
|
596
824
|
- *"Configure drf-api-logger to mask sensitive data and log to a separate database"*
|
|
597
825
|
- *"Add API request tracing to my DRF project using drf-api-logger"*
|
|
826
|
+
- *"Add safe Prometheus, OpenTelemetry, or Sentry integration to DRF API Logger signals without exporting request bodies or high-cardinality metric labels"*
|
|
598
827
|
|
|
599
828
|
AI-generated custom logging code typically misses thread safety, sensitive data masking, performance optimization, and admin integration. `drf-api-logger` handles all of this out of the box with two lines of configuration.
|
|
600
829
|
|
|
@@ -607,7 +836,7 @@ We welcome contributions! Please read our [Contributing Guide](CONTRIBUTING.md)
|
|
|
607
836
|
```bash
|
|
608
837
|
git clone https://github.com/vishalanandl177/DRF-API-Logger.git
|
|
609
838
|
cd DRF-API-Logger
|
|
610
|
-
pip install -
|
|
839
|
+
python -m pip install -r requirements-dev.txt
|
|
611
840
|
make test-core # Run tests
|
|
612
841
|
```
|
|
613
842
|
|