checche / drf-logger

Make logging easily, request_id, status_code, user_id, etc., with Django Rest Framework.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

drf-logger

GitHub Actions PyPI Version MIT License GitHub Starts GitHub Forks

Features

  • Logging satus_code, user_id, the name of function, message from view, etc just by attaching a decorator.
  • Readable formatters for logging.Logger like SimpleExtraFormatter, JSONExtraFormatter.

Description

  • This is a Python package that can easily get information such as status_code, user_id, the name of function etc. just by attaching a decorator.

Installation

pip install drf-logger

Example

  • Write your API with Django Rest Framework and drf-logger.
import drf_logger
from rest_framework.decorators import api_view
from rest_framework.response import Response

# Load logging.Logger object.
logger = drf_logger.utils.get_default_logger(__name__)
# Create api_logger decorator.
api_logger = drf_logger.decorators.APILoggingDecorator(logger)


@api_view(['GET'])
@api_logger
def hello_api(request):
    message = 'This is a message from hello_api.'
    additional = {'message': message}
    return Response({'message': 'hello'}), additional
  • Then, you can get log like follows.
This is a message from hello_api., function=app.views.hello_api, user_id=1, status_code=200
  • You can use APILoggingDecorator in ModeViewSet too.
class PersonViewSet(viewsets.ModelViewSet):

    queryset = Person.objects.all()
    serializer_class = serializers.PersonSerializer

    @api_logger
    def list(self, request):
        queryset = Person.objects.all()
        serializer = serializers.PersonSerializer(queryset, many=True)
        additional = {'message': 'message from list', 'level': 'WARNING'}
        return Response(serializer.data), additional
message from list, function=app.views.PersonViewSet.list, user_id=1, status_code=200

Contributing

A procedure to register to PyPI.

# Install dependencies.
pip install twine
pip install check-manifest
# Check files before upload.
check-manifest
pip install wheel
# Build
python setup.py sdist bdist_wheel
# Upload to PyPI.
twine upload dist/*

Development

# in project root
% docker-compose up # you can develop drf-logger while running sample django app. http://localhost:8000

About

Make logging easily, request_id, status_code, user_id, etc., with Django Rest Framework.

License:MIT License


Languages

Language:Python 99.3%Language:Dockerfile 0.7%