Last active
October 12, 2018 09:30
-
-
Save Alexx-G/2a2986f2e8d93f554fb8 to your computer and use it in GitHub Desktop.
A Django middleware for logging exceptions.
This file contains 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: 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