如何将 Sphinx 警告重定向到 stdout 但在 stderr 中保留错误

How can I redirect Sphinx warnings to stdout but keep errors in stderr

在 Sphinx 构建中,所有警告和错误都写在 stderr 中。但是,我只想要 stderr 中写入的错误和 stdout 中写入的警告。

有什么办法可以实现吗?

这无法更改,除非您将 sphinx 作为一个包加载并对其机器进行猴子修补:

sphinx\cmdline.py:

<...>
def main(argv=sys.argv[1:]):
<...>
    status = sys.stdout
    warning = sys.stderr
    error = sys.stderr

    if args.quiet:
        status = None

    if args.really_quiet:
        status = warning = None

    if warning and args.warnfile:
        try:
            warnfp = open(args.warnfile, 'w')
        <...>
        warning = Tee(warning, warnfp)  # type: ignore
        error = warning

    <...>
        with patch_docutils(), docutils_namespace():
            app = Sphinx(srcdir, confdir, outdir, doctreedir, args.builder,
                         confoverrides, status, warning, args.freshenv,
                         args.warningiserror, args.tags, args.verbosity, args.jobs)
            app.build(args.force_all, filenames)
            return app.statuscode

如您所见,Sphinx 甚至没有针对错误和警告的单独构造函数参数。