简体   繁体   English

Python日志记录默认是写入stdout还是stderr?

[英]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.

相关问题 在Python中记录和旋转stdout和stderr - Logging and rotating stdout and stderr in Python 在Python守护程序中维护日志记录和/或stdout / stderr - Maintaining Logging and/or stdout/stderr in Python Daemon 登录 python 时包含 stderr 和 stdout - Include stderr and stdout when logging in python 在 Python 中使用 with 语句时,将 stderr 和 stdout 重定向到文件不会写入错误 - Redirecting stderr and stdout to a file does not write errors when using with statements in Python 为什么 Python 日志记录默认写入标准错误? - Why Python logging writes to stderr by default? 使用 Popen 启动的 Python 服务有记录 stderr 和 stdout 重定向问题 - Python Service launched with Popen have logging stderr & stdout redirection problems 使用Python'swith'语句将stdout和stderr记录到日志文件中 - Logging stdout and stderr to log file using Python 'with' statement Python 捕获 stdout/stderr 并在查看输出时记录到文件 - Python capturing stdout/stderr and logging to file while seeing output 为什么python库(例如imaplib)不使用日志记录,而是使用sys.stderr.write? - Why does python libs (eg imaplib) does not use logging but use sys.stderr.write? Python asyncio子进程连续写入stdin和读取stdout / stderr - Python asyncio subprocess write stdin and read stdout/stderr continuously
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM