[英]Does Python logging write to stdout or stderr by default?
The logging docs don't mention what the default logger obtained from basicConfig
writes to: stdout or stderr. 日志文档没有提到从
basicConfig
获取的默认记录器写入的内容:stdout或stderr。
What is the default behavior? 什么是默认行为?
Apparently the default is stderr. 显然默认是stderr。
A quick check: Using a minimal example 快速检查:使用最小的例子
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
logger.info("test")
and running it with python test.py 1> /tmp/log_stdout 2> /tmp/log_stderr
results in an empty stdout file, but a non-empty stderr file. 并使用
python test.py 1> /tmp/log_stdout 2> /tmp/log_stderr
运行它python test.py 1> /tmp/log_stdout 2> /tmp/log_stderr
产生一个空的stdout文件,但是会出现一个非空的stderr文件。
If no filename
argument is passed to logging.basicconfig it will configure a StreamHandler
. 如果没有将
filename
参数传递给logging.basicconfig ,它将配置StreamHandler
。 If a stream
argument is passed to logging.basicconfig
it will pass this on to StreamHandler
otherwise StreamHandler
defaults to using sys.stderr
as can be seen from the StreamHandler docs 如果将一个
stream
参数传递给logging.basicconfig
,它将把它传递给StreamHandler
否则StreamHandler
默认使用sys.stderr
,这可以从StreamHandler文档中看到
class logging.StreamHandler(stream=None)
class logging.StreamHandler(stream = None)
Returns a new instance of the StreamHandler class.
返回StreamHandler类的新实例。 If stream is specified, the instance will use it for logging output;
如果指定了stream,则实例将使用它来记录输出; otherwise, sys.stderr will be used.
否则,将使用sys.stderr。
and the 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.