<commit_before>from __future__ import division, absolute_import
from datetime import datetime
import socket, time, logging, sys, traceback, Cookie
import xml.utils.iso8601

from django.contrib.auth.models import User
from django.conf import settings

logger = logging.getLogger('molly.stats.requests')

class StatisticsMiddleware(object):
    def process_request(self, request):
        request.requested = time.time()

    def process_view(self, request, view_func, view_args, view_kwargs):

        request._stats_view_name = ".".join((view_func.__module__, view_func.__name__))

        try:
            request._stats_local_name = view_func.conf.local_name
        except AttributeError, e:
            request._stats_local_name = None

    def process_response(self, request, response):
        logger.info("Request", extra=self.request_details(request, response))
        return response

    def process_exception(self, request, exception):
        details = self.request_details(request)
        details['traceback'] = traceback.format_exc()

        logger.error("Uncaught exception", extra=details)

    def request_details(self, request, response=None):

        view_name = getattr(request, '_stats_view_name', None)
        local_name = getattr(request, '_stats_local_name', None)

        if hasattr(request, 'device'):
            devid = request.device.devid
        else:
            devid = None

        if hasattr(request, 'session'):
            session_key = request.session.session_key
        else:
            try:
                session_key = Cookie.BaseCookie(request.META.get('HTTP_COOKIE', ''))['sessionid'].value
            except:
                session_key = ''

        requested = getattr(request, 'requested', time.time())

        return {
            'session_key': session_key,
            'user_agent': request.META.get('HTTP_USER_AGENT'),
            'device_id': devid,<commit_msg>Http404 and PermissionDenied exceptions are fine -- don't log them as exceptions.<commit_after>from __future__ import division, absolute_import
from datetime import datetime
import socket, time, logging, sys, traceback, Cookie
import xml.utils.iso8601

from django.contrib.auth.models import User
from django.conf import settings
from django.http import Http404
from django.core.exceptions import PermissionDenied

logger = logging.getLogger('molly.stats.requests')

class StatisticsMiddleware(object):
    def process_request(self, request):
        request.requested = time.time()

    def process_view(self, request, view_func, view_args, view_kwargs):

        request._stats_view_name = ".".join((view_func.__module__, view_func.__name__))

        try:
            request._stats_local_name = view_func.conf.local_name
        except AttributeError, e:
            request._stats_local_name = None

    def process_response(self, request, response):
        logger.info("Request", extra=self.request_details(request, response))
        return response

    def process_exception(self, request, exception):
        if isinstance(exception, Http404):
            details = self.request_details(request, type('', (), {'status_code':404}))
            logger.info("Request", extra=details)
        if isinstance(exception, PermissionDenied):
            details = self.request_details(request, type('', (), {'status_code':403}))
            logger.info("Request", extra=details)
        else:
            details = self.request_details(request)
            details['traceback'] = traceback.format_exc()
            logger.error("Uncaught exception", extra=details)

    def request_details(self, request, response=None):

        view_name = getattr(request, '_stats_view_name', None)
        local_name = getattr(request, '_stats_local_name', None)

        if hasattr(request, 'device'):
            devid = request.device.devid
        else:
            devid = None

        if hasattr(request, 'session'):
            session_key = request.session.session_key
        else:
            try:
                session_key = Cookie.BaseCookie(request.META.get('HTTP_COOKIE', ''))['sessionid'].value
            except:
                session_key = ''

        requested = getattr(request, 'requested', time.time())

        return {
            'session_key': session_key,
            'user_agent': request.META.get('HTTP_USER_AGENT'),
            'device_id': devid,
{'fraction_non_alphanumeric': 0.10464058234758872, 'fraction_numerical': 0.005232029117379436, 'mean_word_length': 2.9399641577060933, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '795206', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1172, 'n_words': 266}