Skip to content

Instantly share code, notes, and snippets.

@Alexx-G
Last active October 12, 2018 09:30
Show Gist options
  • Save Alexx-G/2a2986f2e8d93f554fb8 to your computer and use it in GitHub Desktop.
Save Alexx-G/2a2986f2e8d93f554fb8 to your computer and use it in GitHub Desktop.
A Django middleware for logging exceptions.
# -*- coding: utf8 -*-
from __future__ import unicode_literals
import logging
import sys
import traceback
LOGGER = logging.getLogger(__name__)
class ExceptionLoggingMiddleware(object):
"""
This middleware provides logging of exception in requests.
"""
def process_exception(self, request, exception):
"""
Processes exceptions during handling of a http request.
Logs them with *ERROR* level.
"""
_, _, stacktrace = sys.exc_info()
LOGGER.error(
"""Processing exception %s at %s.
GET %s
Traceback %s""",
exception, request.path, request.GET,
''.join(traceback.format_tb(stacktrace)))
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment