Skip to main content

Django middleware for custom format logging

Project description

django-custom-logging

Test PyPI PyPI - Python Version PyPI - License Code style: black

Django middleware for custom format logging

Installation

  1. Install the package
python -m pip install django-custom-logging
  1. Add adequate middlewares to MIDDLEWARE in setting file. Current version only supports a middleware that captures request into local thread(threading.local())
MIDDLEWARE = (
    # other middlewares ...
    "custom_logging.middlewares.capture_request",
)
  1. Add custom_logging.filters.CustomFilter to LOGGING in setting file and set capture_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.5
  • Django: >=3

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-custom-logging-0.1.1.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

django_custom_logging-0.1.1-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file django-custom-logging-0.1.1.tar.gz.

File metadata

  • Download URL: django-custom-logging-0.1.1.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for django-custom-logging-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c9876169aa0765d62f3e06a6c8d2d88ff97addba9a3bb6cd59c416c0d47a2861
MD5 f222077372bc71b7c4d43702a0a0597a
BLAKE2b-256 91b28f9e0da0c87d0f43632697e999fa5d62ba409d2a54c7007c00cb196ad89a

See more details on using hashes here.

File details

Details for the file django_custom_logging-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: django_custom_logging-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for django_custom_logging-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f9d6538dcfafb65e606f2baaa0dbd9369189e44d7410876934a9c389860fafaf
MD5 4c48b7e156abfd8c700873d5daf00278
BLAKE2b-256 be7fb2908912c4458772e55d0b180a5532c2956490c13ad840fe80699fac4c44

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page