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 ...
"custom_logging.middlewares.capture_request",
)
Available Middlewares:
- capture_request
- (TBD)
- Add
custom_logging.filters.CustomFilter
toLOGGING
in setting file and setcapture_list
containing a list of variables to be captured(capture_in
) and format string to be printed(capture_out
). Also add filter on handler's filter list.
For example,
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"verbose": {
"format": "{levelname} {asctime} {module} {process:d} {thread:d}"
" [USER_ID:{user_id}] {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"]},
}
Note that you can use any format styles(%, {, $), but should make format arguments with str
type.
%-style: %(user_id)s
{-style: {user_id}
$-style: ${user_id}
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,170 credentials 35052 4748750336 [USER_ID:-] Found credentials in shared credentials file: ~/.aws/credentials
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.1b7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 276d4406bdc8434531fbe61b322d0ea63feb232079fd289f22a853b3c8ed4760 |
|
MD5 | 3ec09c837d72c92141c36ad61073476f |
|
BLAKE2b-256 | b543c8307b72b2a05134fb971bb90aa51956a76137050e1cef99cb60bfdc98ff |
Close
Hashes for django_custom_logging-0.0.1b7-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04c509013f184fe0b2e18b1c589de5ae8d20d2c0aa40c360155ac03bcdfddeaf |
|
MD5 | 2db353697df6ebea2c22a6c7b69b6555 |
|
BLAKE2b-256 | b3ce80a50ee607c8a56466dd0dc1e751c76b3a862226fd4bc84aaf6af8ebf943 |