Skip to content

Instantly share code, notes, and snippets.

@SehgalDivij
Last active September 10, 2024 20:08
Show Gist options
  • Select an option

  • Save SehgalDivij/1ca5c647c710a2c3a0397bce5ec1c1b4 to your computer and use it in GitHub Desktop.

Select an option

Save SehgalDivij/1ca5c647c710a2c3a0397bce5ec1c1b4 to your computer and use it in GitHub Desktop.

Revisions

  1. SehgalDivij revised this gist Nov 24, 2019. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion middleware.py
    Original file line number Diff line number Diff line change
    @@ -41,7 +41,7 @@ def extract_log_info(self, request, response=None, exception=None):
    if request.method in ['PUT', 'POST', 'PATCH']:
    log_data['request_body'] = json.loads(
    str(request.req_body, 'utf-8'))
    if not response:
    if response:
    if response['content-type'] == 'application/json':
    response_body = response.content
    log_data['response_body'] = response_body
  2. SehgalDivij created this gist Nov 15, 2017.
    65 changes: 65 additions & 0 deletions middleware.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    """
    Middleware to log all requests and responses.
    Uses a logger configured by the name of django.request
    to log all requests and responses according to configuration
    specified for django.request.
    """
    # import json
    import logging
    from django.utils.deprecation import MiddlewareMixin

    import socket
    import time
    import json

    request_logger = logging.getLogger('django.request')


    class RequestLogMiddleware(MiddlewareMixin):
    """Request Logging Middleware."""

    def __init__(self, *args, **kwargs):
    """Constructor method."""
    super().__init__(*args, **kwargs)

    def process_request(self, request):
    """Set Request Start Time to measure time taken to service request."""
    if request.method in ['POST', 'PUT', 'PATCH']:
    request.req_body = request.body
    if str(request.get_full_path()).startswith('/api/'):
    request.start_time = time.time()

    def extract_log_info(self, request, response=None, exception=None):
    """Extract appropriate log info from requests/responses/exceptions."""
    log_data = {
    'remote_address': request.META['REMOTE_ADDR'],
    'server_hostname': socket.gethostname(),
    'request_method': request.method,
    'request_path': request.get_full_path(),
    'run_time': time.time() - request.start_time,
    }
    if request.method in ['PUT', 'POST', 'PATCH']:
    log_data['request_body'] = json.loads(
    str(request.req_body, 'utf-8'))
    if not response:
    if response['content-type'] == 'application/json':
    response_body = response.content
    log_data['response_body'] = response_body
    return log_data

    def process_response(self, request, response):
    """Log data using logger."""
    if request.method != 'GET':
    if str(request.get_full_path()).startswith('/api/'):
    log_data = self.extract_log_info(request=request,
    response=response)
    request_logger.debug(msg='', extra=log_data)
    return response

    def process_exception(self, request, exception):
    """Log Exceptions."""
    try:
    raise exception
    except Exception:
    request_logger.exception(msg="Unhandled Exception")
    return exception