drf-api-logger 1.2.1__tar.gz → 1.2.3__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.1 → drf_api_logger-1.2.3}/PKG-INFO +100 -34
- drf_api_logger-1.2.1/drf_api_logger.egg-info/PKG-INFO → drf_api_logger-1.2.3/README.md +81 -60
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/admin.py +74 -11
- drf_api_logger-1.2.3/drf_api_logger/management/__init__.py +1 -0
- drf_api_logger-1.2.3/drf_api_logger/management/commands/__init__.py +1 -0
- drf_api_logger-1.2.3/drf_api_logger/management/commands/prune_api_logs.py +122 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/migrations/0001_initial.py +1 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/migrations/0003_apilogsmodel_profiling.py +2 -2
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/models.py +0 -2
- drf_api_logger-1.2.3/drf_api_logger/templates/change_form.html +96 -0
- drf_api_logger-1.2.1/README.md → drf_api_logger-1.2.3/drf_api_logger.egg-info/PKG-INFO +126 -24
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger.egg-info/SOURCES.txt +5 -3
- drf_api_logger-1.2.3/drf_api_logger.egg-info/requires.txt +3 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger.egg-info/top_level.txt +0 -1
- drf_api_logger-1.2.3/setup.py +59 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/tests/test_backward_compat.py +6 -1
- drf_api_logger-1.2.3/tests/test_management_commands.py +123 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/tests/test_models.py +85 -1
- drf_api_logger-1.2.1/drf_api_logger/templates/change_form.html +0 -8
- drf_api_logger-1.2.1/drf_api_logger.egg-info/requires.txt +0 -2
- drf_api_logger-1.2.1/setup.py +0 -39
- drf_api_logger-1.2.1/tests/__init__.py +0 -3
- drf_api_logger-1.2.1/tests/urls.py +0 -35
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/LICENSE +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/MANIFEST.in +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/__init__.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/apps.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/collectstatic.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/events.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/insert_log_into_database.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/middleware/__init__.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/middleware/api_logger_middleware.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/migrations/0002_auto_20211221_2155.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/migrations/__init__.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/static/drf_api_logger/css/Chart.min.css +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/static/drf_api_logger/js/Chart.bundle.min.js +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/templates/charts_change_list.html +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/tests.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger/utils.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/drf_api_logger.egg-info/dependency_links.txt +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/pyproject.toml +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/setup.cfg +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/tests/test_integration.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/tests/test_media_url_issue.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/tests/test_middleware.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/tests/test_profiling.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/tests/test_settings.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/tests/test_signals.py +0 -0
- {drf_api_logger-1.2.1 → drf_api_logger-1.2.3}/tests/test_utils.py +0 -0
|
@@ -1,26 +1,34 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: drf-api-logger
|
|
3
|
-
Version: 1.2.
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 1.2.3
|
|
4
|
+
Summary: Production-ready API logging and profiling for Django REST Framework.
|
|
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: Source, https://github.com/vishalanandl177/DRF-API-Logger
|
|
11
|
+
Project-URL: Issue Tracker, https://github.com/vishalanandl177/DRF-API-Logger/issues
|
|
9
12
|
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
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
14
|
Classifier: Programming Language :: Python :: 3.10
|
|
15
15
|
Classifier: Programming Language :: Python :: 3.11
|
|
16
16
|
Classifier: Programming Language :: Python :: 3.12
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
-
Classifier:
|
|
18
|
+
Classifier: Framework :: Django
|
|
19
|
+
Classifier: Framework :: Django :: 4.2
|
|
20
|
+
Classifier: Framework :: Django :: 5.0
|
|
21
|
+
Classifier: Framework :: Django :: 5.1
|
|
22
|
+
Classifier: Framework :: Django :: 5.2
|
|
23
|
+
Classifier: Framework :: Django :: 6.0
|
|
19
24
|
Classifier: Operating System :: OS Independent
|
|
20
|
-
|
|
25
|
+
Classifier: Topic :: Internet :: WWW/HTTP
|
|
26
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
27
|
+
Requires-Python: >=3.10
|
|
21
28
|
Description-Content-Type: text/markdown
|
|
22
29
|
License-File: LICENSE
|
|
23
|
-
Requires-Dist:
|
|
30
|
+
Requires-Dist: Django>=4.2
|
|
31
|
+
Requires-Dist: djangorestframework>=3.16
|
|
24
32
|
Requires-Dist: bleach>=3.1.5
|
|
25
33
|
Dynamic: author
|
|
26
34
|
Dynamic: author-email
|
|
@@ -30,21 +38,31 @@ Dynamic: description-content-type
|
|
|
30
38
|
Dynamic: home-page
|
|
31
39
|
Dynamic: license
|
|
32
40
|
Dynamic: license-file
|
|
41
|
+
Dynamic: project-url
|
|
33
42
|
Dynamic: requires-dist
|
|
34
43
|
Dynamic: requires-python
|
|
35
44
|
Dynamic: summary
|
|
36
45
|
|
|
37
46
|
# DRF API Logger
|
|
38
47
|
|
|
39
|
-
[](https://github.com/vishalanandl177/DRF-API-Logger)
|
|
49
|
+
[](https://www.python.org)
|
|
50
|
+
[](https://djangoproject.com)
|
|
51
|
+
[](https://www.django-rest-framework.org)
|
|
43
52
|
[](http://pepy.tech/project/drf-api-logger)
|
|
44
53
|
[](https://opensource.org/licenses/Apache-2.0)
|
|
45
54
|
|
|
46
55
|
**The production standard for DRF API observability.** Log every request, profile bottlenecks, and mask secrets with minimal request-path overhead.
|
|
47
56
|
|
|
57
|
+
## Supported Versions
|
|
58
|
+
|
|
59
|
+
- Python 3.10+
|
|
60
|
+
- Django 4.2+
|
|
61
|
+
- Django REST Framework 3.16+
|
|
62
|
+
|
|
63
|
+
The GitHub Actions workflow tests representative Django versions from this
|
|
64
|
+
support range before publishing a package release.
|
|
65
|
+
|
|
48
66
|
## 🚀 Key Features
|
|
49
67
|
|
|
50
68
|
DRF API Logger automatically captures and stores comprehensive API information:
|
|
@@ -103,6 +121,33 @@ If using database logging, run migrations:
|
|
|
103
121
|
python manage.py migrate
|
|
104
122
|
```
|
|
105
123
|
|
|
124
|
+
> **Upgrade warning for large MySQL/MariaDB tables:** Version 1.2.0+ adds
|
|
125
|
+
> profiling-related columns (`profiling_data` and `sql_query_count`) to the
|
|
126
|
+
> `drf_api_logs` table. On large MySQL/MariaDB tables, adding columns can take
|
|
127
|
+
> locks or require table rebuilds depending on the database version, storage
|
|
128
|
+
> engine, row format, and existing table definition. Plan this migration like a
|
|
129
|
+
> production schema change.
|
|
130
|
+
>
|
|
131
|
+
> Before upgrading, inspect the SQL Django will run:
|
|
132
|
+
>
|
|
133
|
+
> ```bash
|
|
134
|
+
> python manage.py sqlmigrate drf_api_logger 0003
|
|
135
|
+
> ```
|
|
136
|
+
>
|
|
137
|
+
> For large MySQL/MariaDB deployments, validate the generated SQL against your
|
|
138
|
+
> exact database/version, prefer database-native online DDL where supported, and
|
|
139
|
+
> consider manually adding the columns with a safe online schema migration tool
|
|
140
|
+
> or database-native online DDL. If the columns are added manually, fake-apply
|
|
141
|
+
> the Django migration afterward:
|
|
142
|
+
>
|
|
143
|
+
> ```bash
|
|
144
|
+
> python manage.py migrate drf_api_logger 0003 --fake
|
|
145
|
+
> ```
|
|
146
|
+
>
|
|
147
|
+
> Avoid copying a generic `ALTER TABLE` command without validating it for your
|
|
148
|
+
> database. MySQL and MariaDB online DDL behavior differs by version and table
|
|
149
|
+
> definition.
|
|
150
|
+
|
|
106
151
|
## ⚙️ Quick Start
|
|
107
152
|
|
|
108
153
|
### Database Logging
|
|
@@ -441,16 +486,19 @@ The package includes comprehensive test coverage:
|
|
|
441
486
|
|
|
442
487
|
```bash
|
|
443
488
|
# Install test dependencies
|
|
444
|
-
pip install -
|
|
489
|
+
pip install -r requirements-dev.txt
|
|
445
490
|
|
|
446
491
|
# Run core tests
|
|
447
492
|
python test_runner_simple.py
|
|
448
493
|
|
|
449
|
-
# Run full test suite
|
|
450
|
-
python
|
|
494
|
+
# Run full test suite
|
|
495
|
+
python -m django test tests --settings=tests.test_settings --verbosity=1
|
|
496
|
+
|
|
497
|
+
# Run supported Python/Django/DRF combinations
|
|
498
|
+
tox
|
|
451
499
|
|
|
452
500
|
# With coverage
|
|
453
|
-
coverage run --source drf_api_logger
|
|
501
|
+
coverage run --source drf_api_logger -m django test tests --settings=tests.test_settings --verbosity=1
|
|
454
502
|
coverage report
|
|
455
503
|
```
|
|
456
504
|
|
|
@@ -479,13 +527,20 @@ For high-traffic applications:
|
|
|
479
527
|
CREATE INDEX idx_api_logs_api_method ON drf_api_logs(api, method);
|
|
480
528
|
```
|
|
481
529
|
|
|
482
|
-
4. **
|
|
483
|
-
```
|
|
484
|
-
#
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
530
|
+
4. **Prune old data** periodically:
|
|
531
|
+
```bash
|
|
532
|
+
# Preview rows older than 30 days
|
|
533
|
+
python manage.py prune_api_logs --days 30 --dry-run
|
|
534
|
+
|
|
535
|
+
# Delete rows older than 30 days in batches
|
|
536
|
+
python manage.py prune_api_logs --days 30 --batch-size 1000
|
|
537
|
+
```
|
|
538
|
+
|
|
539
|
+
You can also prune before a fixed date:
|
|
540
|
+
|
|
541
|
+
```bash
|
|
542
|
+
python manage.py prune_api_logs --before 2026-06-01 --dry-run
|
|
543
|
+
python manage.py prune_api_logs --before 2026-06-01
|
|
489
544
|
```
|
|
490
545
|
|
|
491
546
|
### Performance Impact
|
|
@@ -494,6 +549,17 @@ For high-traffic applications:
|
|
|
494
549
|
- **Observable queue backlog** via `LOGGER_THREAD.get_status()` for health checks
|
|
495
550
|
- **Efficient storage** (bulk database operations)
|
|
496
551
|
|
|
552
|
+
Example health check:
|
|
553
|
+
|
|
554
|
+
```python
|
|
555
|
+
from drf_api_logger.apps import LOGGER_THREAD
|
|
556
|
+
|
|
557
|
+
def drf_logger_status():
|
|
558
|
+
if LOGGER_THREAD is None:
|
|
559
|
+
return {"enabled": False}
|
|
560
|
+
return {"enabled": True, **LOGGER_THREAD.get_status()}
|
|
561
|
+
```
|
|
562
|
+
|
|
497
563
|
### Compliance Readiness
|
|
498
564
|
|
|
499
565
|
For regulated or privacy-sensitive deployments, set conservative payload limits,
|
|
@@ -506,15 +572,15 @@ deployment checklist.
|
|
|
506
572
|
|
|
507
573
|
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:
|
|
508
574
|
|
|
509
|
-
| Problem | Custom Logging | drf-api-logger
|
|
510
|
-
|
|
511
|
-
| **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
|
|
512
|
-
| **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
|
|
513
|
-
| **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
|
|
514
|
-
| **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
|
|
575
|
+
| Problem | Custom Logging | drf-api-logger |
|
|
576
|
+
|---|---|---------------------------------------------------------------------------------------------------------------------------------|
|
|
577
|
+
| **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 |
|
|
578
|
+
| **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 |
|
|
579
|
+
| **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 |
|
|
580
|
+
| **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 |
|
|
515
581
|
| **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 |
|
|
516
|
-
| **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
|
|
517
|
-
| **Maintenance burden** | Every Django/DRF upgrade risks breaking your custom middleware |
|
|
582
|
+
| **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 |
|
|
583
|
+
| **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 |
|
|
518
584
|
|
|
519
585
|
**Bottom line:** `pip install drf-api-logger` replaces hundreds of lines of fragile custom code with a production-tested, zero-config solution.
|
|
520
586
|
|
|
@@ -580,7 +646,7 @@ We welcome contributions! Please read our [Contributing Guide](CONTRIBUTING.md)
|
|
|
580
646
|
```bash
|
|
581
647
|
git clone https://github.com/vishalanandl177/DRF-API-Logger.git
|
|
582
648
|
cd DRF-API-Logger
|
|
583
|
-
pip install -
|
|
649
|
+
python -m pip install -r requirements-dev.txt
|
|
584
650
|
make test-core # Run tests
|
|
585
651
|
```
|
|
586
652
|
|
|
@@ -1,50 +1,23 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: drf-api-logger
|
|
3
|
-
Version: 1.2.1
|
|
4
|
-
Summary: An API Logger for your Django Rest Framework project.
|
|
5
|
-
Home-page: https://github.com/vishalanandl177/DRF-API-Logger
|
|
6
|
-
Author: Vishal Anand
|
|
7
|
-
Author-email: vishalanandl177@gmail.com
|
|
8
|
-
License: Apache 2.0
|
|
9
|
-
Classifier: Programming Language :: Python :: 3
|
|
10
|
-
Classifier: Programming Language :: Python :: 3.6
|
|
11
|
-
Classifier: Programming Language :: Python :: 3.7
|
|
12
|
-
Classifier: Programming Language :: Python :: 3.8
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
15
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
16
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
17
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
-
Classifier: License :: OSI Approved :: Apache Software License
|
|
19
|
-
Classifier: Operating System :: OS Independent
|
|
20
|
-
Requires-Python: >=3.6
|
|
21
|
-
Description-Content-Type: text/markdown
|
|
22
|
-
License-File: LICENSE
|
|
23
|
-
Requires-Dist: djangorestframework>=3.7.4
|
|
24
|
-
Requires-Dist: bleach>=3.1.5
|
|
25
|
-
Dynamic: author
|
|
26
|
-
Dynamic: author-email
|
|
27
|
-
Dynamic: classifier
|
|
28
|
-
Dynamic: description
|
|
29
|
-
Dynamic: description-content-type
|
|
30
|
-
Dynamic: home-page
|
|
31
|
-
Dynamic: license
|
|
32
|
-
Dynamic: license-file
|
|
33
|
-
Dynamic: requires-dist
|
|
34
|
-
Dynamic: requires-python
|
|
35
|
-
Dynamic: summary
|
|
36
|
-
|
|
37
1
|
# DRF API Logger
|
|
38
2
|
|
|
39
|
-
[](https://github.com/vishalanandl177/DRF-API-Logger)
|
|
4
|
+
[](https://www.python.org)
|
|
5
|
+
[](https://djangoproject.com)
|
|
6
|
+
[](https://www.django-rest-framework.org)
|
|
43
7
|
[](http://pepy.tech/project/drf-api-logger)
|
|
44
8
|
[](https://opensource.org/licenses/Apache-2.0)
|
|
45
9
|
|
|
46
10
|
**The production standard for DRF API observability.** Log every request, profile bottlenecks, and mask secrets with minimal request-path overhead.
|
|
47
11
|
|
|
12
|
+
## Supported Versions
|
|
13
|
+
|
|
14
|
+
- Python 3.10+
|
|
15
|
+
- Django 4.2+
|
|
16
|
+
- Django REST Framework 3.16+
|
|
17
|
+
|
|
18
|
+
The GitHub Actions workflow tests representative Django versions from this
|
|
19
|
+
support range before publishing a package release.
|
|
20
|
+
|
|
48
21
|
## 🚀 Key Features
|
|
49
22
|
|
|
50
23
|
DRF API Logger automatically captures and stores comprehensive API information:
|
|
@@ -103,6 +76,33 @@ If using database logging, run migrations:
|
|
|
103
76
|
python manage.py migrate
|
|
104
77
|
```
|
|
105
78
|
|
|
79
|
+
> **Upgrade warning for large MySQL/MariaDB tables:** Version 1.2.0+ adds
|
|
80
|
+
> profiling-related columns (`profiling_data` and `sql_query_count`) to the
|
|
81
|
+
> `drf_api_logs` table. On large MySQL/MariaDB tables, adding columns can take
|
|
82
|
+
> locks or require table rebuilds depending on the database version, storage
|
|
83
|
+
> engine, row format, and existing table definition. Plan this migration like a
|
|
84
|
+
> production schema change.
|
|
85
|
+
>
|
|
86
|
+
> Before upgrading, inspect the SQL Django will run:
|
|
87
|
+
>
|
|
88
|
+
> ```bash
|
|
89
|
+
> python manage.py sqlmigrate drf_api_logger 0003
|
|
90
|
+
> ```
|
|
91
|
+
>
|
|
92
|
+
> For large MySQL/MariaDB deployments, validate the generated SQL against your
|
|
93
|
+
> exact database/version, prefer database-native online DDL where supported, and
|
|
94
|
+
> consider manually adding the columns with a safe online schema migration tool
|
|
95
|
+
> or database-native online DDL. If the columns are added manually, fake-apply
|
|
96
|
+
> the Django migration afterward:
|
|
97
|
+
>
|
|
98
|
+
> ```bash
|
|
99
|
+
> python manage.py migrate drf_api_logger 0003 --fake
|
|
100
|
+
> ```
|
|
101
|
+
>
|
|
102
|
+
> Avoid copying a generic `ALTER TABLE` command without validating it for your
|
|
103
|
+
> database. MySQL and MariaDB online DDL behavior differs by version and table
|
|
104
|
+
> definition.
|
|
105
|
+
|
|
106
106
|
## ⚙️ Quick Start
|
|
107
107
|
|
|
108
108
|
### Database Logging
|
|
@@ -441,16 +441,19 @@ The package includes comprehensive test coverage:
|
|
|
441
441
|
|
|
442
442
|
```bash
|
|
443
443
|
# Install test dependencies
|
|
444
|
-
pip install -
|
|
444
|
+
pip install -r requirements-dev.txt
|
|
445
445
|
|
|
446
446
|
# Run core tests
|
|
447
447
|
python test_runner_simple.py
|
|
448
448
|
|
|
449
|
-
# Run full test suite
|
|
450
|
-
python
|
|
449
|
+
# Run full test suite
|
|
450
|
+
python -m django test tests --settings=tests.test_settings --verbosity=1
|
|
451
|
+
|
|
452
|
+
# Run supported Python/Django/DRF combinations
|
|
453
|
+
tox
|
|
451
454
|
|
|
452
455
|
# With coverage
|
|
453
|
-
coverage run --source drf_api_logger
|
|
456
|
+
coverage run --source drf_api_logger -m django test tests --settings=tests.test_settings --verbosity=1
|
|
454
457
|
coverage report
|
|
455
458
|
```
|
|
456
459
|
|
|
@@ -479,13 +482,20 @@ For high-traffic applications:
|
|
|
479
482
|
CREATE INDEX idx_api_logs_api_method ON drf_api_logs(api, method);
|
|
480
483
|
```
|
|
481
484
|
|
|
482
|
-
4. **
|
|
483
|
-
```
|
|
484
|
-
#
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
485
|
+
4. **Prune old data** periodically:
|
|
486
|
+
```bash
|
|
487
|
+
# Preview rows older than 30 days
|
|
488
|
+
python manage.py prune_api_logs --days 30 --dry-run
|
|
489
|
+
|
|
490
|
+
# Delete rows older than 30 days in batches
|
|
491
|
+
python manage.py prune_api_logs --days 30 --batch-size 1000
|
|
492
|
+
```
|
|
493
|
+
|
|
494
|
+
You can also prune before a fixed date:
|
|
495
|
+
|
|
496
|
+
```bash
|
|
497
|
+
python manage.py prune_api_logs --before 2026-06-01 --dry-run
|
|
498
|
+
python manage.py prune_api_logs --before 2026-06-01
|
|
489
499
|
```
|
|
490
500
|
|
|
491
501
|
### Performance Impact
|
|
@@ -494,6 +504,17 @@ For high-traffic applications:
|
|
|
494
504
|
- **Observable queue backlog** via `LOGGER_THREAD.get_status()` for health checks
|
|
495
505
|
- **Efficient storage** (bulk database operations)
|
|
496
506
|
|
|
507
|
+
Example health check:
|
|
508
|
+
|
|
509
|
+
```python
|
|
510
|
+
from drf_api_logger.apps import LOGGER_THREAD
|
|
511
|
+
|
|
512
|
+
def drf_logger_status():
|
|
513
|
+
if LOGGER_THREAD is None:
|
|
514
|
+
return {"enabled": False}
|
|
515
|
+
return {"enabled": True, **LOGGER_THREAD.get_status()}
|
|
516
|
+
```
|
|
517
|
+
|
|
497
518
|
### Compliance Readiness
|
|
498
519
|
|
|
499
520
|
For regulated or privacy-sensitive deployments, set conservative payload limits,
|
|
@@ -506,15 +527,15 @@ deployment checklist.
|
|
|
506
527
|
|
|
507
528
|
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:
|
|
508
529
|
|
|
509
|
-
| Problem | Custom Logging | drf-api-logger
|
|
510
|
-
|
|
511
|
-
| **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
|
|
512
|
-
| **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
|
|
513
|
-
| **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
|
|
514
|
-
| **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
|
|
530
|
+
| Problem | Custom Logging | drf-api-logger |
|
|
531
|
+
|---|---|---------------------------------------------------------------------------------------------------------------------------------|
|
|
532
|
+
| **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 |
|
|
533
|
+
| **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 |
|
|
534
|
+
| **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 |
|
|
535
|
+
| **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 |
|
|
515
536
|
| **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 |
|
|
516
|
-
| **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
|
|
517
|
-
| **Maintenance burden** | Every Django/DRF upgrade risks breaking your custom middleware |
|
|
537
|
+
| **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 |
|
|
538
|
+
| **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 |
|
|
518
539
|
|
|
519
540
|
**Bottom line:** `pip install drf-api-logger` replaces hundreds of lines of fragile custom code with a production-tested, zero-config solution.
|
|
520
541
|
|
|
@@ -580,7 +601,7 @@ We welcome contributions! Please read our [Contributing Guide](CONTRIBUTING.md)
|
|
|
580
601
|
```bash
|
|
581
602
|
git clone https://github.com/vishalanandl177/DRF-API-Logger.git
|
|
582
603
|
cd DRF-API-Logger
|
|
583
|
-
pip install -
|
|
604
|
+
python -m pip install -r requirements-dev.txt
|
|
584
605
|
make test-core # Run tests
|
|
585
606
|
```
|
|
586
607
|
|
|
@@ -4,10 +4,46 @@ from django.conf import settings
|
|
|
4
4
|
from django.contrib import admin
|
|
5
5
|
from django.db.models import Count, Avg
|
|
6
6
|
from django.http import HttpResponse
|
|
7
|
+
from django.utils.html import escape
|
|
7
8
|
from django.utils.safestring import mark_safe
|
|
8
9
|
from drf_api_logger.utils import database_log_enabled
|
|
9
10
|
|
|
10
11
|
|
|
12
|
+
def _prettify_json_field(value):
|
|
13
|
+
"""
|
|
14
|
+
Render a stored log field (headers / body / response) as a pretty-printed,
|
|
15
|
+
HTML-escaped JSON block for the admin change form.
|
|
16
|
+
|
|
17
|
+
The middleware already stores these as indented JSON, but plain/large or
|
|
18
|
+
non-JSON payloads (truncation markers, XML, streaming placeholders, ...) are
|
|
19
|
+
handled gracefully. The returned markup is wrapped in ``<pre class="apilogs-json">``
|
|
20
|
+
which the change-form template syntax-highlights on the client side. No size
|
|
21
|
+
limit is applied here so even large bodies are prettified in full.
|
|
22
|
+
"""
|
|
23
|
+
if value is None or value == '':
|
|
24
|
+
return mark_safe('<div class="apilogs-empty">— empty —</div>')
|
|
25
|
+
|
|
26
|
+
text = value if isinstance(value, str) else str(value)
|
|
27
|
+
stripped = text.strip()
|
|
28
|
+
|
|
29
|
+
# Special placeholders emitted by the middleware, e.g.
|
|
30
|
+
# "** Response body truncated: 70000 bytes exceeds 65536 byte limit **".
|
|
31
|
+
is_marker = stripped.startswith('**') and stripped.endswith('**')
|
|
32
|
+
|
|
33
|
+
badge = ''
|
|
34
|
+
if is_marker and 'truncated' in stripped.lower():
|
|
35
|
+
badge = '<div class="apilogs-truncated">TRUNCATED</div>'
|
|
36
|
+
|
|
37
|
+
pretty = text
|
|
38
|
+
if not is_marker:
|
|
39
|
+
try:
|
|
40
|
+
pretty = json.dumps(json.loads(text), indent=2, ensure_ascii=False)
|
|
41
|
+
except (ValueError, TypeError):
|
|
42
|
+
pretty = text # leave non-JSON payloads (XML, plain text, ...) as-is
|
|
43
|
+
|
|
44
|
+
return mark_safe('{}<pre class="apilogs-json">{}</pre>'.format(badge, escape(pretty)))
|
|
45
|
+
|
|
46
|
+
|
|
11
47
|
def _get_profiling_diagnosis(profiling):
|
|
12
48
|
"""Generate a diagnosis hint based on profiling data patterns."""
|
|
13
49
|
total = profiling.get('view_and_serialization', 0)
|
|
@@ -40,6 +76,12 @@ def _get_profiling_diagnosis(profiling):
|
|
|
40
76
|
return None
|
|
41
77
|
|
|
42
78
|
|
|
79
|
+
def _normalized_filter_value(value):
|
|
80
|
+
if isinstance(value, (list, tuple)):
|
|
81
|
+
return value[-1] if value else None
|
|
82
|
+
return value
|
|
83
|
+
|
|
84
|
+
|
|
43
85
|
# Ensure the API log model and related features are only used if enabled in settings
|
|
44
86
|
if database_log_enabled():
|
|
45
87
|
from drf_api_logger.models import APILogsModel
|
|
@@ -108,9 +150,10 @@ if database_log_enabled():
|
|
|
108
150
|
Returns filtered queryset depending on whether 'slow' or 'fast'
|
|
109
151
|
option is selected in the filter.
|
|
110
152
|
"""
|
|
111
|
-
|
|
153
|
+
value = _normalized_filter_value(self.value())
|
|
154
|
+
if value == 'slow':
|
|
112
155
|
return queryset.filter(execution_time__gte=self._DRF_API_LOGGER_SLOW_API_ABOVE)
|
|
113
|
-
if
|
|
156
|
+
if value == 'fast':
|
|
114
157
|
return queryset.filter(execution_time__lt=self._DRF_API_LOGGER_SLOW_API_ABOVE)
|
|
115
158
|
return queryset
|
|
116
159
|
|
|
@@ -128,13 +171,14 @@ if database_log_enabled():
|
|
|
128
171
|
)
|
|
129
172
|
|
|
130
173
|
def queryset(self, request, queryset):
|
|
131
|
-
|
|
174
|
+
value = _normalized_filter_value(self.value())
|
|
175
|
+
if value == 'high':
|
|
132
176
|
return queryset.filter(sql_query_count__gte=10)
|
|
133
|
-
if
|
|
177
|
+
if value == 'moderate':
|
|
134
178
|
return queryset.filter(sql_query_count__gte=5, sql_query_count__lt=10)
|
|
135
|
-
if
|
|
179
|
+
if value == 'low':
|
|
136
180
|
return queryset.filter(sql_query_count__lt=5, sql_query_count__isnull=False)
|
|
137
|
-
if
|
|
181
|
+
if value == 'none':
|
|
138
182
|
return queryset.filter(sql_query_count__isnull=True)
|
|
139
183
|
return queryset
|
|
140
184
|
|
|
@@ -174,10 +218,13 @@ if database_log_enabled():
|
|
|
174
218
|
self.list_filter += (HighQueryCountFilter,)
|
|
175
219
|
self.readonly_fields = (
|
|
176
220
|
'execution_time', 'client_ip_address', 'api',
|
|
177
|
-
'
|
|
178
|
-
'added_on_time', 'profiling_breakdown',
|
|
221
|
+
'headers_prettified', 'body_prettified', 'method', 'response_prettified',
|
|
222
|
+
'status_code', 'added_on_time', 'profiling_breakdown',
|
|
223
|
+
)
|
|
224
|
+
self.exclude = (
|
|
225
|
+
'added_on', 'profiling_data', 'sql_query_count',
|
|
226
|
+
'headers', 'body', 'response',
|
|
179
227
|
)
|
|
180
|
-
self.exclude = ('added_on', 'profiling_data', 'sql_query_count')
|
|
181
228
|
|
|
182
229
|
def added_on_time(self, obj):
|
|
183
230
|
"""
|
|
@@ -311,6 +358,21 @@ if database_log_enabled():
|
|
|
311
358
|
|
|
312
359
|
profiling_breakdown.short_description = 'Profiling Breakdown'
|
|
313
360
|
|
|
361
|
+
def headers_prettified(self, obj):
|
|
362
|
+
return _prettify_json_field(obj.headers)
|
|
363
|
+
|
|
364
|
+
headers_prettified.short_description = 'Headers'
|
|
365
|
+
|
|
366
|
+
def body_prettified(self, obj):
|
|
367
|
+
return _prettify_json_field(obj.body)
|
|
368
|
+
|
|
369
|
+
body_prettified.short_description = 'Body'
|
|
370
|
+
|
|
371
|
+
def response_prettified(self, obj):
|
|
372
|
+
return _prettify_json_field(obj.response)
|
|
373
|
+
|
|
374
|
+
response_prettified.short_description = 'Response'
|
|
375
|
+
|
|
314
376
|
# Admin UI settings
|
|
315
377
|
list_per_page = 20
|
|
316
378
|
list_display = ('id', 'api', 'method', 'status_code', 'execution_time', 'added_on_time',)
|
|
@@ -318,9 +380,10 @@ if database_log_enabled():
|
|
|
318
380
|
search_fields = ('body', 'response', 'headers', 'api',)
|
|
319
381
|
readonly_fields = (
|
|
320
382
|
'execution_time', 'client_ip_address', 'api',
|
|
321
|
-
'
|
|
383
|
+
'headers_prettified', 'body_prettified', 'method', 'response_prettified',
|
|
384
|
+
'status_code', 'added_on_time',
|
|
322
385
|
)
|
|
323
|
-
exclude = ('added_on',)
|
|
386
|
+
exclude = ('added_on', 'headers', 'body', 'response')
|
|
324
387
|
|
|
325
388
|
# Custom admin templates
|
|
326
389
|
change_list_template = 'charts_change_list.html'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|