I am looking for the best possible solution where I can do the following :
If there is a way to do this , how can it be done and how to go about it?
Django has a an event logging functionality which uses Python logger to deliver all the tasks you're looking for. You can read more about it here
While writing an app, you can use the following approach:
Whenever your app raises an Exception, use custom, app-specific exceptions like this
class FooException(Exception):
"""Base class for all app specific exceptions"""
pass
classFooPermissionException(FooException):
"""Specific class for permission related exceptions"""
pass
Now your app can provide a custom middleware
class FooExceptionMiddleware(object):
"""App specific middleware to handle app specific errors"""
def process_exception(self, request, e):
"""Processes exceptions"""
if isinstance(e, FooException):
if isinstance(e, FooPermissionException):
# handle PermissionExceptions here...
return None
You can read more about Django middleware here.
Edit:
I think you can use the above approach, to catch all Exceptions: Have a look at Django's exception source code. Django's internal exceptions are siblings of Exception. So with something like the following code, you can possibly catch all exceptions, wherever they are raised in your project:
class AllExceptionMiddleware(object):
def process_exception(self, request, e):
if isinstance(e, Exception):
# do stuff
return None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With