Django middleware for custom format logging
Project description
django-custom-logging
Django middleware for custom format logging
⚠️ Currently pre-release version is only supported.
Installation
- Install the package
python -m pip install django-custom-logging
- Add adequate middlewares to
MIDDLEWARE
in setting file
MIDDLEWARE = (
# other middlewares ...
"django-custom-logging.middleware.capture_request",
)
Available Middlewares:
- capture_request
- (TBD)
- Add
custom_logging.filters.CustomFilter
toLOGGING
in setting file and update formatter, and add filter on handler's filter. You should put placeholder$DJANGO_CUSTOM_LOGGING
in format string which will be replaced byCustomFilter
.
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"verbose": {
"format": "{levelname} {asctime} {module} {process:d} {thread:d}"
" $DJANGO_CUSTOM_LOGGING {message}",
"style": "{",
},
},
"filters": {
"custom_filter": {
"()": "custom_logging.filters.CustomFilter",
"capture_list": (
# (capture_in, capture_out)
("request.user.id", "user_id"),
),
},
},
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "verbose",
"filters": ["custom_filter"],
},
},
"root": {"level": "INFO", "handlers": ["console"]},
}
Current version only supports "{" style format.
How to use
You can use logger
just like before. No extra parameter is needed.
import logging
from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
logger = logging.getLogger(__name__)
class ExampleView(APIView):
permission_classes = (IsAuthenticated,)
def post(self, request, format=None):
logger.info("example log")
return Response({"hello": "world!"}, status=status.HTTP_200_OK)
INFO 2021-03-25 11:33:25,505 views 35052 4748750336 [USER_ID:33] example log
Supported versions
- Python: >=3.6
- Django: >=3
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for django-custom-logging-0.0.1b5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79159f6fa1dd6cffe0fbdf363593566ec9e6ceae68adfc6ece8e97eda6070150 |
|
MD5 | 861f1aab6015f9e179faa84543b4ebf3 |
|
BLAKE2b-256 | 15c6916e5847f7e42d3020d3615f35af266beedb85b9952a03c7d54730c53509 |
Close
Hashes for django_custom_logging-0.0.1b5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc41d372d31f910b1ccdafea4901bd9fbcda20e349d975cbfb22cce784bed6ba |
|
MD5 | d183220c047bd579bd9889bd8557010f |
|
BLAKE2b-256 | 0844f2f74168e4efb18b5b616aa1f861a0bdb105d0bf9b6f2bee6a6225872ba8 |