Showing the right funcName when wrapping logger functionality in a custom class

前端 未结 8 1695
你的背包
你的背包 2021-02-01 16:31

This is the formatting string that I am using for logging:

\'%(asctime)s - %(levelname)-10s - %(funcName)s - %(message)s\'

But to show the logg

8条回答
  •  感情败类
    2021-02-01 16:49

    First of all according to your code it's clear why it happens, levelname and funcName "belongs" to self.log so when you call to self.log(level, line) the levelname is level and funcName is line.

    You have 2 options IMHO:

    1. To use inspect module to get the current method and to deliver it inside the message, then you can parse it and to use it very easily.

    2. A better approach will be to use inspect inside split_line to get the "father" method you can change the number(3) in the following code to "play" with the hierarchy of the methods.

    example of using inspect to get current method

    from inspect import stack
    
    class Foo:
        def __init__(self):
            print stack()[0][3]
    
    f = Foo()
    

提交回复
热议问题