Python:获取当前位置的模块/类和函数名

Python:获取当前位置的模块/类和函数名,python,logging,Python,Logging,在Python中,我希望在脚本中检索模块/类/函数的实际名称 我该怎么做 背景: 我想在日志中动态打印模块/类/函数的名称 因此,我的首选解决方案是: my_script.py def my_function(): print (xxxxxx) print ("hallo") my_function() 结果: pydev debugger: process 23262 is connecting my_script.py, my_function hallo

在Python中,我希望在脚本中检索模块/类/函数的实际名称

我该怎么做

背景:

我想在日志中动态打印模块/类/函数的名称

因此,我的首选解决方案是:

my_script.py    

def my_function():
    print (xxxxxx)

    print ("hallo")

my_function()
结果:

pydev debugger: process 23262 is connecting
my_script.py, my_function
hallo
编辑:


这将通过回溯模块获取调用堆栈,并获取第二个但也是最后一个条目。路径被截断为模块的名称。

签出请添加一些解释。
def log_name():
    if True:
        print(re.sub(".*/","",str(traceback.extract_stack()[-2])))


def my_function():
    log_name()