Using the python logging module I would like the log record to include the fully qualified name of the function being logged from. I.e. the same information contained in __qualname__
. The logging format string just contains funcName
which is equivalent to __name__
on the function. Am I wishing for the impossible here?
BR Troels
Using the python logging module I would like the log record to include the fully qualified name of the function being logged from. I.e. the same information contained in __qualname__
. The logging format string just contains funcName
which is equivalent to __name__
on the function. Am I wishing for the impossible here?
BR Troels
Share Improve this question asked Feb 5 at 9:49 Troels BlumTroels Blum 8541 gold badge8 silver badges19 bronze badges1 Answer
Reset to default -1I think this code could be what you want.
def testmethod():
import inspect
f = inspect.currentframe()
pre_func = ''
funcs = []
while 1:
try:
f = f.f_back
f_info = inspect.getframeinfo(f)
func = f_info.function
if func != '<module>':
pre_func = func
_funcs = list(f.f_code.co_names)
_funcs.reverse()
funcs += _funcs
else:
funcs.append(pre_func)
funcs.append('__main__')
break
except:
break
funcs.reverse()
print('.'.join(funcs))
def method():
testmethod()
class TestClass:
def classmethod(self):
method()
def methodtop():
a = TestClass()
a.classmethod()
if __name__ == '__main__':
methodtop() # __main__.methodtop.TestClass.classmethod.method.testmethod