django-requestlog / requestlog /

RJ Ryan e23b4ff 

RJ Ryan 403895f 

RJ Ryan e23b4ff 

RJ Ryan 403895f 
RJ Ryan e23b4ff 

RJ Ryan 403895f 

RJ Ryan e23b4ff 

RJ Ryan 403895f 
RJ Ryan 41f699d 

RJ Ryan 403895f 

RJ Ryan e23b4ff 

RJ Ryan 403895f 

RJ Ryan 41f699d 

RJ Ryan 403895f 
RJ Ryan e23b4ff 
import time
import logging
import traceback
import sys

    import json
except ImportError:
    import simplejson as json

from requestlog import handler, models


class LoggingMiddleware(object):

    """Logs exceptions with tracebacks with the standard logging module."""

    def __init__(self):
        self._handler = handler.ThreadBufferedHandler()

    def report_exception(exception_type=None, exception=None, tb=None):
        if exception and exception_type and tb:
            logging.debug("An unhandled exception occured: %s" %
                                                          exception, tb)))
        elif exception:
            logging.error("An unhandled exception occurred: %s" % repr(exception))

    def log_records(self, request):
        time_taken = -1
        if hasattr(request, LOGGING_START_TIME_ATTR):
            start = getattr(request, LOGGING_START_TIME_ATTR)
            time_taken = time.time() - start

        records = self._handler.get_records()
        first = records[0] if len(records) > 0 else None
        records = [self._record_to_json(record, first) for record in records]
        message = json.dumps(records)

        log_entry = models.RequestLog(uri=request.path,

    def process_exception(self, request, exception):
        # exception_type, exception, tb = sys.exc_info()
        # self.report_exception(exception=exception,
        #                       exception_type=exception_type,
        #                       tb=tb)

    def process_request(self, request):
        setattr(request, LOGGING_START_TIME_ATTR, time.time())
        return None

    def _time_humanize(self, seconds):
        return "%.3fs" % seconds

    def _record_delta(self, this, first):
        return self._time_humanize(this - first)

    def _record_to_json(self, record, first):
        return {'filename': record.filename,
                'timestamp': record.created,
                'level_name': record.levelname,
                'level_number': record.levelno,
                'module': record.module,
                'function_name': record.funcName,
                'line_number': record.lineno,
                'message': record.msg,
                'delta': self._record_delta(record.created, first.created)

    def process_response(self, request, response):

        if not hasattr(request, LOGGING_ENABLE_ATTR):
            return response

        # Sometimes an exception will not be propagated so someone downstream
        # can set the exception as a property on the request to make sure we
        # note it.
        if hasattr(request, LOGGING_EXCEPTION):
            exception_type, exception, tb = getattr(request, LOGGING_EXCEPTION)
            # self.report_exception(exception_type=exception_type,
            #                       exception=exception,
            #                       tb=tb)

        return response
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.