【问题标题】:Python needs to log each script into its own logPython 需要将每个脚本记录到自己的日志中
【发布时间】:2019-02-15 00:11:52
【问题描述】:

我有脚本 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...

【问题讨论】:

  • Loggers 也有一个日志级别。对于简单的情况,在记录器上设置 lg 级别,而不是在处理程序上。

标签: python python-2.7


【解决方案1】:

上面的人对记录器的默认级别是正确的。此外,与其将您的配置分散到各处,我发现在应用程序的早期整合日志配置更易于管理。请参阅下面的示例。

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

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)

【讨论】:

  • 是的,它现在可以工作了,非常感谢!但由于某种原因,propagate=False/True 看起来没有任何区别。我评论出来只是为了看看子日志是否也会进入 main.log 但每个脚本只记录到自己的日志......
  • 好吧,如果我还想登录到根日志stackoverflow.com/a/38087696/1890619,看来我需要正确链接它
  • 是的,如果您希望所有内容都转到 main.log 并让子文件转到其他地方,您实际上可能希望将根记录器设置为您的主文件(或类似的文件)。我更新了答案以包含一个示例。我认为也值得一看 logging.basicConfig() 作为设置根记录器的简单方法(尽管您仍然需要执行一些额外的步骤来获取单独的子记录器)。
【解决方案2】:

您可以在处理程序和记录器上设置严重级别 - 我相信记录器默认设置为 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...')

【讨论】:

    猜你喜欢
    • 2012-01-06
    • 1970-01-01
    • 2012-09-14
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    相关资源
    最近更新 更多