I have a decorator to retry some operations for 3 times if a deadlock happens, and does some logging in the process:
def retry_on_deadlock(func):
logger = logging.getLogger('test')
@wraps(func)
def decorated(*args, **kwargs):
retry_count = 0
while retry_count < 3:
try:
return func(*args, **kwargs)
except DeadLockException:
retry_count += 1
if retry_count == 3:
raise
logger.warning('We are in the decorated function here')
return decorated
@retry_on_deadlock
def crucial_function(value):
with value:
do_something()
crucial_function(important_data)
Our logging format includes %(funcName)s
which, in this specific case, will evaluate to decorated
. Is there a way to make crucial_function
appear in the logs instead?
I already tried implementing a logging filter, but the logic it needs became a bit cumbersome, as it needs to inspect the stack, and if the logging happened directly in the decorated
function (ie. not in a function called by the original function) it will overwrite funcName
on the log record. If this is the only way, though, i will sadly accept it.
Update: This is not the same as this question as i don’t really care about the functions signature (which, in fact, is preserved by the @wraps
decorator). I want to instruct the logging
library to skip one level from the stack trace when logging the function’s name via the %(funcName)s
, %(filename)s
, and %(lineno)s
.