Answer the question
In order to leave comments, you need to log in
How to handle all (including those from other threads) exceptions in Django?
I need to log all the errors that happened in the project. Especially the ones that happened in other threads.
In flask, this is done by specifying an error handler:
app.register_error_handler(Exception, _error_handler)
def _error_handler(error):
log.exception(error)
# middleware.py:
class LoggingExceptionMiddleware(MiddlewareMixin):
def process_exception(self, request, exception):
logger.error('LoggingExceptionMiddleware.process_exception was called')
logger.exception(exception)
# views.py
def exception(request):
raise Exception('bla bla')
def exception_in_new_thread(request):
logger.debug('exception_in_new_thread called')
Thread(target=buggy_func).start()
return HttpResponse('ok')
def buggy_func():
logger.debug('buggy_func called')
sleep(5)
raise Exception('hi from buggy_func')
Answer the question
In order to leave comments, you need to log in
I did not find something similar in django.
Options
1. Abandon Thread in django, they don't make much sense, it's better to put such tasks in the job queue
2. Handle all exceptions inside the Thread, wrap it in a decorator for simplicity.
+ to everything above https://docs.djangoproject.com/en/2.2/topics/logging/
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question