gpt4 book ai didi

python - 如何临时重定向 Python 中日志记录的输出?

转载 作者:太空宇宙 更新时间:2023-11-03 11:30:18 24 4
gpt4 key购买 nike

这里已经有一个问题可以回答如何执行有关 sys.stdoutsys.stderr 的问题:https://stackoverflow.com/a/14197079/198348

但这并不适用于任何地方。日志记录模块似乎输出到 sys.stdoutsys.stderr,但我无法使用上面的上下文管理器捕获它。

在下面的示例代码中,我试图捕获上下文管理器中的所有输出,但未能针对记录器语句执行此操作:

from __future__ import print_function
import contextlib
import sys
import logging
from StringIO import StringIO

# taken from https://stackoverflow.com/a/14197079/198348
@contextlib.contextmanager
def stdout_redirect(where):
prev_stdout = sys.stdout
prev_stderr = sys.stderr
prev_stdout.flush()
sys.stdout = where
sys.stderr = where
try:
yield where
finally:
where.flush()
sys.stdout = prev_stdout
sys.stderr = prev_stderr

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger()

print("\t\tOUTSIDE: stdout", file=sys.stdout)
print("\t\tOUTSIDE: stderr", file=sys.stderr)
logger.info("\tOUTSIDE: info")
logger.debug("\tOUTSIDE: debug")
logger.warn("\tOUTSIDE: warn")
logger.error("\tOUTSIDE: error")
logger.critical("\tOUTSIDE: critical")

print("=============== DIVIDER ================")

s = ""
with stdout_redirect(StringIO()) as new_stdout:
print("\t\tINSIDE: stdout", file=sys.stdout)
print("\t\tINSIDE: stderr", file=sys.stderr)
logger.info("\tINSIDE: info")
logger.debug("\tINSIDE: debug")
logger.warn("\tINSIDE: warn")
logger.error("\tINSIDE: error")
logger.critical("\tINSIDE: critical")


print("=============== DIVIDER ===============")
print(new_stdout.getvalue())

print("=============== LOGGING ===============")

print(logger.handlers)
print(logger.root.handlers)

我怎样才能暂时将吐出的记录器的输出重定向到标准输出并捕获它们?我看了一下 logging/init.py ,但它不会立即告诉我需要做什么。

我这样做的动机是我想为一个笨拙的大代码库配备测试,每个测试都会捕获每个测试调用的虚假日志输出量。我可以捕获外部程序,但我似乎无法捕获我在内部运行的测试 nose .

重写冗长的部分现在不是一个选项,但绝对是 future 的目标。

编辑,关于ubuntu

这是我尝试使用 nosetests 运行的内容:

from __future__ import print_function
import sys

def test_funky_shurane():
import logging
logging.basicConfig(level=logging.DEBUG)
logging.info("===== shurane info")
logging.warn("===== shurane warn")
logging.error("===== shurane error")
logging.critical("===== shurane critical")
print("===== shurane stdout", file=sys.stdout)
print("===== shurane stderr", file=sys.stderr)
assert True

然后运行上面的代码:

nosetests test_logging.py
nosetests --nocapture test_logging.py

最佳答案

logging.basicConfig() 非常方便,可以以非常简单的方式设置一些记录器处理。如果您需要的不止于此,则不应使用 basicConfig()。这没什么大不了的,因为它并没有做很多事情。我们需要的是为两个流配置日志记录;

import logging, sys
fmt = logging.Formatter(BASIC_FORMAT)

hdlr_stderr = logging.StreamHandler(sys.stderr)
hdlr_stderr.setFormatter(fmt)
hdlr_stdout = logging.StreamHandler(sys.stdout)
hdlr_stdout.setFormatter(fmt)
root.addHandler(hdlr_stderr)
root.addHandler(hdlr_stdout)
root.setLevel(logging.DEBUG)

默认情况下,记录器会记录它们收到的所有消息;但最初,我们不想将任何消息记录到 sys.stdout:

hdlr_stdout.level = float('inf')  # larger than any log level; nothing gets logged

然后,您的上下文管理器可能看起来有点像:

@contextlib.contextmanager
def redirect_stderr_logging(where):
hdlr_stderr.level = float('inf')
hdlr_stdout.level = logging.NOTSET
try:
yield where
finally:
hdlr_stderr.level = logging.NOTSET
hdlr_stdout.level = float('inf')

关于python - 如何临时重定向 Python 中日志记录的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22105465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com