Jump to >

djblets.log.middleware

Middleware used for logging.

The middleware sets up some advanced logging capabilities for profiling and exception logging.

class CursorDebugWrapper(cursor, db)[source]

Bases: django.db.backends.util.CursorDebugWrapper

Debug cursor for databases that stores tracebacks in query logs.

This is a replacement for the default debug wrapper for database cursors which stores a traceback in connection.queries.

This will dramatically increase the overhead of having settings.DEBUG=True, so use with caution.

execute(*args, **kwargs)[source]

Execute a SQL statement.

This will record an entry in the query log and attach a stack trace.

Parameters:
  • *args (tuple) – Positional arguments for the parent method.
  • **kwargs (dict) – Keyword arguments for the parent method.
Returns:

The result of the statement.

Return type:

object

executemany(*args, **kwargs)[source]

Execute multiple SQL statements.

This will record an entry in the query log and attach a stack trace.

Note that the Django ORM does not appear to actually make use of this method. Third-parties can still use it.

Parameters:
  • *args (tuple) – Positional arguments for the parent method.
  • **kwargs (dict) – Keyword arguments for the parent method.
Returns:

The result of the statement.

Return type:

object

reformat_sql(sql)[source]
class LoggingMiddleware[source]

Bases: object

A piece of middleware that sets up page timing and profile logging.

This is needed if using settings.LOGGING_PAGE_TIMES or settings.LOGGING_ALLOW_PROFILING, in order to handle additional logging for page times and detailed profiling for debugging.

ignored_exceptions = (<class 'django.http.response.Http404'>, <class 'django.core.exceptions.PermissionDenied'>, <class 'django.core.exceptions.SuspiciousOperation'>)[source]

Exceptions that should be ignored by this logger.

Each of these are handled by Django itself on the HTTP layer. We don’t want to do any extra/unwanted logging of these.

process_request(request)[source]

Processes an incoming request. This will set up logging.

process_view(request, callback, callback_args, callback_kwargs)[source]

Handler for processing a view. This will run the profiler on the view if profiling is allowed in the settings and the user specified the profiling parameter on the URL.

process_response(request, response)[source]

Handler for processing a response. Dumps the profiling information to the profile log file.

process_exception(request, exception)[source]

Handle exceptions raised on a page.

Logs the exception, along with the username and path where the exception occurred.

Exceptions normally handled by Django’s HTTP layer will be ignored. This helps avoid extraneous logging for 404 and Permission Denied errors.

Parameters:
  • request (django.http.HttpRequest) – The HTTP request for the page.
  • exception (Exception) – The exception that was raised.