Is there a way to log messages as if they originated from the current function's caller?
I have a very simple global logging facility that gets initialized by means of __main__ as
logformat = '%(asctime)s %(levelname)s %(module)s:%(funcName)s:%(lineno)d - %(message)s' logging.basicConfig(format=logformat, level=loglevel) All functions then use the same global logger directly by means of
# Goes directly to the global logger logging.warning('this might not work') Some utility functions emit logs, but it is rather unhelpful that %(module)s:%(funcName)s:%(lineno)d resolves to helpers:my_helper_fn:42, while the caller of my_helper_fn that triggered the log is the interesting part.
Is there a way to log messages inside my_helper_fn so that funcName and friends automatically resolve to the caller of my_helper_fn? Ideally, I'd decorate my_helper_fn with something to the tune of @logAsCaller...
There is logger.findCaller which seems to be purposefully designed for this, I can't deduce from the docs how it's supposed to be used, though.