Django middleware for custom format logging
Project description
django-custom-logging
Django middleware for custom format logging
Installation
- Install the package
python -m pip install django-custom-logging
- Add adequate middlewares to
MIDDLEWARE
in setting file. Current version only supports a middleware that capturesrequest
into local thread(threading.local()
)
MIDDLEWARE = (
# other middlewares ...
"custom_logging.middlewares.capture_request",
)
- 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.
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"verbose": {
"format": "{levelname} {asctime} {module} {process:d} {thread:d}"
" [USER_ID:{user_id}] {message}",
#^^^^^^^^^ - (A)
"style": "{",
},
},
"filters": {
"custom_filter": {
#^^^^^^^^^^^^^ - (B)
"()": "custom_logging.filters.CustomFilter",
"capture_list": (
# (capture_in, capture_out)
("request.user.id", "user_id"),
#^^^^^^^ - (A)
),
},
},
"handlers": {
"console": {
"level": "DEBUG",
"class": "logging.StreamHandler",
"formatter": "verbose",
"filters": ["custom_filter"],
#^^^^^^^^^^^^^ - (B)
},
},
"root": {"level": "INFO", "handlers": ["console"]},
}
Note that you can use any format styles(%, {, $), but should make format arguments with str
type. For example, if you want to capture request.user.id
as user_id
, please follow format below.
%-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.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 059eb9553b201e013fbded79c3eb87e6316bb1ff2b097fe0260ba854ad72818c |
|
MD5 | 3a4e2b59dd6073d7aebaad7c4941923e |
|
BLAKE2b-256 | bb107ea5da56bac9a3d3bdd00e408a4a349611065a386683ce6970f22e68fee0 |
Close
Hashes for django_custom_logging-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c06fd87225ddb1217420d10e54d8560f69afa27f01ef556833bed8ce01385dfb |
|
MD5 | 5af9d97bf4f9f68bbd9200a4ddc927b1 |
|
BLAKE2b-256 | d39f45eca7776e0e5a67acda9683a47ef2f53921765f874b34f625f1906c7dc3 |