python.logging: Why does my non-basicConfig setting not work?

后端 未结 1 1251
遥遥无期
遥遥无期 2020-12-21 17:10

I want to log to a single log file from main and all sub modules.

The log messages send from a main file, where I define the logger, work as expected. But the ones s

相关标签:
1条回答
  • 2020-12-21 17:54

    You need to configure the (one and only) root logger in the main module of your software. This is done by calling

    logger = logging.getLogger() #without arguments
    

    instead of

    logger = logging.getLogger(__name__)
    

    (Python doc on logging)

    The second example creates a separate, child logger with the name of your script.

    If there are no handlers defined in the submodules, the log message is being passed down to the root logger to handle it.

    A related question can be found here: Python Logging - How to inherit root logger level & handler

    0 讨论(0)
提交回复
热议问题