【发布时间】:2020-09-19 06:19:29
【问题描述】:
我编写了一个 Python 包,它使用 logging 模块,并广泛使用了带有 Python 包装器的第三方 C++ 库。我已经能够将我自己的包中的消息打印到控制台,以及将它们写入文件(并且每个处理程序具有不同的日志记录级别)。但是,我想在我的日志文件中包含第三方库打印的消息,以便查看它们出现的顺序。这是我的 MWE:
import logging
# Assume no direct access to this function. (e.g. c++ library)
def third_party_function():
print("Inside of 'third_party_function'.")
def my_helper():
logger.debug("Inside of 'my_helper', before third party call.")
third_party_function()
logger.warning("Finished with third party call.")
root_logger = logging.getLogger()
root_logger.setLevel(logging.NOTSET)
logger = logging.getLogger("mylogger")
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.WARNING)
file_handler = logging.FileHandler(filename="progress.out")
file_handler.setLevel(logging.NOTSET)
logger.addHandler(stream_handler)
logger.addHandler(file_handler)
my_helper()
就目前而言,屏幕的输出是:
Inside of 'third_party_function'.
Finished with third party call.
文件progress.out包含
Inside of 'my_helper', before third party call.
Finished with third party call.
但是,想要的 progress.out 文件是
Inside of 'my_helper', before third party call.
Inside of 'third_party_function'.
Finished with third party call.
似乎没有属于这个第三方库的记录器,因为它不是用 Python 编写的。
我希望避免将sys.stdout 设置为文件(如here),因为我希望保持一致并始终使用logging 模块。 Another answer 为同一个问题定义了一个自定义类,但这似乎仍然没有捕捉到第三方消息。
【问题讨论】: