最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Python logging format: is it possible to get the fully qualified function name (including class) as a part f the format string?

programmeradmin9浏览0评论

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 badges
Add a comment  | 

1 Answer 1

Reset to default -1

I 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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论