gpt4 book ai didi

python - Python 日志记录是否默认写入 stdout 或 stderr?

转载 作者:太空狗 更新时间:2023-10-30 00:58:15 27 4
gpt4 key购买 nike

logging docs不要提及默认记录器从 basicConfig 获得的内容写入:stdout 或 stderr。

默认行为是什么?

最佳答案

如果没有 filename 参数传递给 logging.basicconfig它将配置一个StreamHandler。如果将 stream 参数传递给 logging.basicconfig,它将把它传递给 StreamHandler,否则 StreamHandler 默认使用sys.stderrStreamHandler docs可以看出

class logging.StreamHandler(stream=None)

Returns a new instance of the StreamHandler class. If stream is specified, the instance will use it for logging output; otherwise, sys.stderr will be used.

source code :

class StreamHandler(Handler):
"""
A handler class which writes logging records, appropriately formatted,
to a stream. Note that this class does not close the stream, as
sys.stdout or sys.stderr may be used.
"""

def __init__(self, stream=None):
"""
Initialize the handler.
If stream is not specified, sys.stderr is used.
"""
Handler.__init__(self)
if stream is None:
stream = sys.stderr
self.stream = stream

关于python - Python 日志记录是否默认写入 stdout 或 stderr?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56496458/

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