I'd like to write a function that basically adds a log entry in addition to raising a given Exception
, e.g.
def logged_raise(exc, *args):
try:
raise exc(args)
except exc:
logging.exception(args)
# Or something more complicated, e.g. printing the
# stacktrace if loggig.isEnabledFor(logging.DEBUG)
raise
And call logged_raise(MyException, 'some %s text', myvar)
instead of raise MyException('some %s text', myvar)
.
While this basically works, the traceback points to the raise exc(args)
line in logged_raise
instead of the logged_raise(MyException,...)
call. Can this be fixed somehow?