I wrote a decorator to log any exception a function might throw.
import logging
def log_exception(func):
def func_new(*args, **kwargs):
try:
return func(*args, **kwargs)
except Exception as e:
logging.error(str(e))
raise e
return func_new
It is used like this:
@log_exception
def divide(a, b):
return a/b
For example, divide(1, 0)
would log 'division by zero'
.
My question is whether there's a builtin or more robust way to achieve the same thing. Maybe in the logging
or contextlib
modules?