Logging as the caller module in Python

后端 未结 3 596
离开以前
离开以前 2021-01-25 03:21

I have the following application structure:

./utils.py
def do_something(logger=None):
    if not logger:
        logger = logging.getLogger(__name__)

    print(         


        
3条回答
  •  迷失自我
    2021-01-25 03:32

    If you don't like the existing solutions, here's a magic way to do it:

    def do_something():
        logger = inspect.currentframe().f_back.f_globals['log']
        ...
    

    The calling function must then have a global variable called log.

提交回复
热议问题