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

我有脚本 parent.py 和 child.py(许多孩子),我需要为每个脚本创建日志,因此 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...


繁星coding
浏览 134回答 2
2回答

慕沐林林

上面的人对记录器的默认级别是正确的。此外,我发现将日志配置整合到应用程序的早期阶段更易于管理,而不是将您的配置分散到任何地方。请参阅下面的示例。注意:我不希望这被选为答案。我只是想指出我认为组织代码的更好方法。主文件import loggingimport childlogger = 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()孩子.pyimport logginglogger = logging.getLogger(__name__)def child_func():    logger.info('testing child...')设置根记录器和子记录器(无主记录器)这是一个设置根记录器以登录到的示例logs/main.log,以及将子记录器设置为转到的示例logs/child.logdef 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)

POPMUISE

您可以在处理程序和记录器上设置严重性级别 - 我相信记录器logging.WARNING默认设置为,因此您只能使用代码获得警告日志。import loggingimport childhandler = 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)&nbsp; &nbsp; &nbsp; &nbsp;# <-- changed&nbsp;child.child_func()logger.info('testing parent...')logger.warning('testing parent...')logger.debug('testing parent...')#child.pyimport logginghandler = 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)&nbsp; &nbsp; &nbsp; # <-- changeddef child_func():&nbsp; &nbsp; logger.info('testing child...')&nbsp; &nbsp; logger.warning('testing child...')&nbsp; &nbsp; logger.debug('testing child...')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python