为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

繁星coding 2021-11-09 16:24:01
我有脚本 parent.py 和 child.py(许多孩子),我需要为每个脚本创建日志,因此 parent.py 中的任何日志记录都应该在 parent.log 中,child.py 应该在 child.log 中我在每个脚本中都有以下内容,但我得到空日志......为什么?#main.pyimport childhandler = 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.pyhandler = 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...
查看完整描述

2 回答

?
慕沐林林

TA贡献2016条经验 获得超9个赞

上面的人对记录器的默认级别是正确的。此外,我发现将日志配置整合到应用程序的早期阶段更易于管理,而不是将您的配置分散到任何地方。请参阅下面的示例。


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


主文件

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()

孩子.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)


查看完整回答
反对 回复 2021-11-09
?
POPMUISE

TA贡献1765条经验 获得超5个赞

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

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...')


查看完整回答
反对 回复 2021-11-09
  • 2 回答
  • 0 关注
  • 127 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信