Skip to content
Snippets Groups Projects
Commit ab2682b4 authored by dcz's avatar dcz
Browse files

Log post data

parent b92cfb3c
Branches
No related tags found
No related merge requests found
......@@ -80,6 +80,7 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'syntax.middleware.SessionLogMiddleware',
'django.middleware.locale.LocaleMiddleware',
]
......
import json
import logging
import traceback
from django.http import Http404
from django.http.request import RawPostDataException
from django.utils.deprecation import MiddlewareMixin
# if you want to track it
# from sentry_sdk import capture_exception
# def get_client_ip(request):
# """
# get client ip address
#
# used in:
# - apps/models_helper/visitor.py
# """
# ip_address = request.META.get('HTTP_X_FORWARDED_FOR', None)
# if ip_address:
# ip_address = ip_address.split(', ')[0]
# else:
# ip_address = request.META.get('REMOTE_ADDR', '')
# return ip_address
class SessionLogMiddleware(MiddlewareMixin):
"""
this middleware to create a log
by current user
"""
def process_exception(self, request, exception):
# print(type(exception), exception) # just for debug
status_code = 404 if isinstance(exception, Http404) else 500
try:
# try:
# request_data = json.loads(str(request.POST))
# except RawPostDataException:
# response_data = "RawPostDataException: You cannot access body after reading from request's data stream"
# except Exception:
# try:
# request_data = str(request.body)
# except Exception:
# pass
print("user: id={}, name={}".format(request.user.id, request.user.username))
print("request_data: {}".format(request.POST))
print("exception: {}".format(exception))
# print("stck: {}".format(traceback.format_exc(exception)))
# self.save_mongodb_logging(request,
# exception=exception,
# status_code=status_code)
except Exception as error:
# error = traceback.format_exc()
# capture_exception(error)
print(error)
return # no return anything
# def process_response(self, request, response):
# if not response.status_code >= 400:
# try:
# response_data = {'request': request,
# 'response': response,
# 'status_code': response.status_code}
# self.save_mongodb_logging(**response_data)
# except Exception:
# pass
# return response
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment