【问题标题】:How to have file name and line number in GAE Python logging.info logs?如何在 GAE Python logging.info 日志中获取文件名和行号?
【发布时间】:2015-02-11 21:27:13
【问题描述】:

在日志中,时间戳是可用的,但"source file name""line number" 不会与每条日志消息一起记录。

如何在 GAE Python logging.info(或 logging.warnlogging.error 等)中记录文件名和行号?

"source file name" 是指生成日志的 Python 代码文件 (*.py)。

【问题讨论】:

    标签: python google-app-engine python-2.7


    【解决方案1】:

    在你的main.py 添加:

    formatter = logging.Formatter('%(levelname)-8s %(asctime)s %(filename)s:%(lineno)s] %(message)s')
    
    logging.getLogger().handlers[0].setFormatter(formatter)
    

    【讨论】:

      【解决方案2】:

      你试过看logging formatters吗?

      它应该看起来像这样

      formatter = logging.Formatter(
          '[%(asctime)s] p%(process)s {%(pathname)s:%(lineno)d}'
          ' %(levelname)s - %(message)s','%m-%d %H:%M:%S')
      

      【讨论】:

        猜你喜欢
        • 2017-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-29
        • 2016-12-21
        • 2022-08-02
        • 2011-10-05
        • 1970-01-01
        相关资源
        最近更新 更多