【问题标题】:How to redirect abseil logging messages to stout instead of stderr?如何将abseil日志消息重定向到stout而不是stderr?
【发布时间】:2020-05-18 12:39:54
【问题描述】:

我正在使用python 3.7.6。和用于记录消息的abseil模块with absl-py0.9.0。我正在使用这段代码进行测试。

from absl import logging
from absl import app

def main(argv):

    #logging.set_stderrthreshold(logging.ERROR)
    #logging._warn_preinit_stderr = False
    logging.set_verbosity(logging.DEBUG)

    print(' 0 -----')
    logging.debug(' 1 logging-debug-test')
    logging.info(' 2 logging-info-test')
    logging.warning(' 3 logging-warning-test')
    logging.error('4 logging-error-test')
    print(' 5 -----')

if __name__ == '__main__':
    app.run(main)

Jupyter notebook 中对其进行测试时,从背景的颜色代码中可以清楚地看出,abseil 消息在 stderr 流中。

在 shell 中执行 python 代码时也是这样:
我尝试了一些具有不同值的东西,例如:

logging.set_stderrthreshold(logging.DEBUG)
logging._warn_preinit_stderr = True

但我仍然看到 100% 相同的输出。

  • 如何将输出的 abseil 日志消息重定向到 stdout 而不是 stderr ?
  • 是否希望将日志记录输出消息重定向到 stderr 而不是 stdout?我可能遗漏了一些日志记录逻辑,我想更好地理解它。

【问题讨论】:

    标签: python-3.x logging stdout abseil absl-py


    【解决方案1】:

    有人告诉我这是标准行为,也是 Python 的标准日志记录模块的作用。在我的情况下,添加以下行将日志消息重定向到标准输出:

    logging.get_absl_handler().python_handler.stream = sys.stdout
    

    现在在我的 Jupyter 笔记本中,它看起来像这样:

    【讨论】:

      【解决方案2】:

      由于某种原因,这对我不起作用:

      from absl import logging
      import sys
      
      logging.get_absl_handler().python_handler.stream = sys.stdout
      

      但是这样做了:

      import logging
      import sys
      
      logging.basicConfig(stream=sys.stdout)
      

      【讨论】:

        猜你喜欢
        • 2017-05-31
        • 2011-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-02-04
        • 2019-09-11
        相关资源
        最近更新 更多