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.1b6.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | f66eba181fe3d4ab3d8d05458267813346850f3b964163522dc0ff7be0d94466 |
|
MD5 | 2b08ec149fa3f717ac4d0895be7cfabe |
|
BLAKE2b-256 | ef6f77d080a27dba3308e9a939630e859f05f750670daaa6ea7be8c0e2aa74fc |
Close
Hashes for django_custom_logging-0.0.1b6-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b90c90442c8b0795707c2d5d03c1d0c94bf72716438d823a7fc065e5a7b84b5 |
|
MD5 | 7138471bc16855cf70e82f168f85cf40 |
|
BLAKE2b-256 | 49c79aa37c7249c4cb23537d1f50f139e0467c7f033957da7cc52579934dd9ab |