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} {meta[REMOTE_ADDR]} {meta[CONTENT_LENGTH]} {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)
("request.META", "meta"),
),
"default_values" : {
"meta": {
"REMOTE_ADDR": "127.0.0.1",
"CONTENT_LENGTH": 0,
}
},
},
},
"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}
⚠️ Specifying Default Values
Default values should be provided if capture_out
is object and its attribute can be undefined. (ex. {meta[REMOTE_ADDR]}
)
For example, accessing request variable in filter while using scheduling cronjob(ex. @shared_task
) can raise error because it is not HTTP request so request
is not defined.
If capture_out
is single value(ex. str, int, etc.), default value is not needed. It will be replaced with placeholder -
if it is not defined.
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 2024-03-01 11:33:25,170 credentials 127.0.0.1 0 35052 4748750336 [USER_ID:-] Found credentials in shared credentials file: ~/.aws/credentials
INFO 2024-03-01 11:33:25,505 views 123.123.123.123 1000 35052 4748750336 [USER_ID:33] example log
Supported versions
- Python: >=3.5
- Django: >=3
License
See LICENSE
Contribution
Feel free to open issue or pull request.
Contributors
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
Hashes for django_custom_logging-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b341a888c79b6a1bd90984f2a1e8cb3454374a1aa06f5e0aa9c6b0b774de0db5 |
|
MD5 | 01fe5e7d8fbb026c6f159049a5ee7142 |
|
BLAKE2b-256 | 6b83f1e2573ec49e02e9d617f2350bd39d821a4b41ff147216b78d704f6ae97f |
Hashes for django_custom_logging-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5591842c9918ff4e11d0672c72416c7a1faf7f9718ad8ac1ee4c92a2a1a10a6a |
|
MD5 | 1a9bc715237fc7476344d52c6f815f47 |
|
BLAKE2b-256 | e47933a2c3d2baf1be51f12aff5169b3bb7af68541a5d7c75812c4b838c3fa8f |