Python需要将每个脚本记录到自己的日志中

2024-04-19 08:59:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我有parent.py和child.py脚本(许多child),并且我需要为每个脚本都创建日志,因此parent.py中的任何日志都应该在parent.log中,child.py应该在child.log中

我有下面的每个脚本,但我得到空日志。。。为什么

#main.py
import child

handler = logging.FileHandler('logs/main.log')
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - % 
(funcName)10s()] %(levelname)s: %(message)s")
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(handler)

child.child_func()
logger.info('testing parent...')


#child.py

handler = logging.FileHandler('logs/child.log')
handler.setLevel(logging.DEBUG)
formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - % 
(funcName)10s()] %(levelname)s: %(message)s")
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(handler)

def child_func():
    logger.info('testing child...')

我需要的是

#parent.log
{format} testing parent...

#child.log
{format} testing child...

Tags: pydebug脚本logchildmainformatterlogging
2条回答

上面的人对记录器的默认级别是正确的。另外,我发现在应用程序的早期整合日志记录配置更容易管理,而不是到处散布配置。请参见下面的示例

注意:我不希望这个被选为答案。我只是想指出我认为组织代码的更好方法

main.py

import logging
import child


logger = logging.getLogger(__name__)


def setup_logging():
    main_handler = logging.FileHandler('logs/main.log')
    child_handler = logging.FileHandler('logs/child.log')

    # Note that you can re-use the same formatter for the handlers.
    formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - %(funcName)10s()] %(levelname)s: %(message)s")

    main_handler.setFormatter(formatter)
    child_handler.setFormatter(formatter)

    # By default, loggers inherit the level from their parent, so we only need
    # to set the level on the root logger if you want to have only one knob to
    # control the level.
    root_logger = logging.getLogger()
    root_logger.setLevel(logging.DEBUG)

    main_logger = logging.getLogger(__name__)
    child_logger = logging.getLogger('child')
    child_logger.propagate = False

    main_logger.addHandler(main_handler)
    child_logger.addHandler(child_handler)


def main():
    setup_logging()

    child.child_func()
    logger.info('testing parent...')


if __name__ == '__main__':
    main()

child.py

import logging


logger = logging.getLogger(__name__)


def child_func():
    logger.info('testing child...')

设置根记录器和子记录器(无主记录器)

下面是一个设置根记录器到logs/main.log,子记录器到logs/child.log的示例

def setup_logging():
    root_handler = logging.FileHandler('logs/main.log')
    child_handler = logging.FileHandler('logs/child.log')

    # Note that you can re-use the same formatter for the handlers.
    formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - %(funcName)10s()] %(levelname)s: %(message)s")

    root_handler.setFormatter(formatter)
    child_handler.setFormatter(formatter)

    # By default, loggers inherit the level from their parent, so we only need
    # to set the level on the root logger if you want to have only one knob to
    # control the level.
    root_logger = logging.getLogger()
    root_logger.setLevel(logging.DEBUG)
    root_logger.addHandler(root_handler)

    child_logger = logging.getLogger('child')
    child_logger.propagate = False
    child_logger.addHandler(child_handler)

您可以在处理程序和记录器上设置严重性级别—我相信记录器在默认情况下设置为logging.WARNING,因此您只能使用代码获取警告日志

您可以在以下线程中阅读更多内容:What is the point of setLevel in a python logging handler?

import logging
import child

handler = logging.FileHandler('logs/main.log')
formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - %(funcName)10s()] %(levelname)s: %(message)s")
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)       # <  changed 
child.child_func()
logger.info('testing parent...')
logger.warning('testing parent...')
logger.debug('testing parent...')

#child.py
import logging

handler = logging.FileHandler('logs/child.log')
formatter = logging.Formatter("%(asctime)s [%(filename)s:%(lineno)s - %(funcName)10s()] %(levelname)s: %(message)s")
handler.setFormatter(formatter)
logger = logging.getLogger(__name__)
logger.addHandler(handler)
logger.setLevel(logging.DEBUG)      # <  changed
def child_func():
    logger.info('testing child...')
    logger.warning('testing child...')
    logger.debug('testing child...')

相关问题 更多 >