Created
December 19, 2012 02:56
-
-
Save moqada/4334002 to your computer and use it in GitHub Desktop.
logging POST data for under Django 1.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| import os | |
| import logging | |
| import logging.handlers | |
| from django.conf import settings | |
| from django.utils import simplejson as json | |
| class POSTScanningMiddleware(object): | |
| def __init__(self): | |
| self.logger = logging.getLogger('postlog') | |
| handler = logging.handlers.RotatingFileHandler( | |
| os.path.join(settings.LOG_DIR, 'post.log'), maxBytes=100000000, backupCount=5) | |
| formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s') | |
| handler.setFormatter(formatter) | |
| self.logger.propagate = False | |
| self.logger.addHandler(handler) | |
| def process_view(self, request, view_func, view_args, view_kwargs): | |
| if request.method == 'POST': | |
| data = { | |
| 'PATH': request.path_info, | |
| 'POST': request.POST, | |
| 'META': { | |
| 'REMOTE_ADDR': request.META.get('REMOTE_ADDR', ''), | |
| 'HTTP_REFERER': request.META.get('HTTP_REFERER', ''), | |
| 'HTTP_USER_AGENT': request.META.get('HTTP_USER_AGENT', ''), | |
| } | |
| } | |
| self.logger.info(json.dumps(data)) | |
| return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment