Python stderr的扭曲和写入异常

Python stderr的扭曲和写入异常,python,exception,error-handling,twisted,stderr,Python,Exception,Error Handling,Twisted,Stderr,我正在尝试将Twisted和Python结合使用。我需要将异常和错误导出到一个文件,而不是控制台,以防在运行时出现任何意外情况,我碰巧错过了它,但是重定向stderr似乎不起作用。该错误仍然显示在控制台中,并且不会写入文件(尽管文件已创建) 下面是一个简单的例子: from twisted.internet import reactor import sys sys.stderr = open('error.log', 'a') def error_test(): int("

我正在尝试将Twisted和Python结合使用。我需要将异常和错误导出到一个文件,而不是控制台,以防在运行时出现任何意外情况,我碰巧错过了它,但是重定向stderr似乎不起作用。该错误仍然显示在控制台中,并且不会写入文件(尽管文件已创建)

下面是一个简单的例子:

from twisted.internet import reactor
import sys

sys.stderr = open('error.log', 'a')

def error_test():
    int("Hello")

reactor.callLater(1, error_test)

reactor.run()

我做错了什么?

您可以使用
twist
(或
twistd
)来配置日志记录。例如:

$ twist --log-file /tmp/some-file --log-format=text --log-level info web
^C
$ cat /tmp/some-file 
2020-10-18T10:35:41-0400 [-] Site starting on 8080
2020-10-18T10:35:41-0400 [twisted.web.server.Site#info] Starting factory <twisted.web.server.Site instance at 0x7fe59bbf4d70>
2020-10-18T10:35:41-0400 [twisted.application.runner._runner.Runner#info] Starting reactor...
2020-10-18T10:35:44-0400 [-] Received SIGINT, shutting down.
2020-10-18T10:35:44-0400 [-] (TCP Port 8080 Closed)
2020-10-18T10:35:44-0400 [twisted.web.server.Site#info] Stopping factory <twisted.web.server.Site instance at 0x7fe59bbf4d70>
2020-10-18T10:35:44-0400 [-] Main loop terminated.
$
import sys

from twisted.logger import globalLogBeginner
from twisted.logger import textFileLogObserver

globalLogBeginner.beginLoggingTo([textFileLogObserver(sys.stderr)])

from twisted.internet import reactor

def error_test():
    int("Hello")

reactor.callLater(1, error_test)

reactor.run()
它将日志发送到stderr,如下所示:

$ python /tmp/demo.py 2>/tmp/demo-err
^C
$ cat /tmp/demo-err 
2020-10-18T10:43:15-0400 [-] Unhandled Error
        Traceback (most recent call last):
          File "/tmp/demo.py", line 15, in <module>
            reactor.run()
          File "twisted/internet/base.py", line 1283, in run
            self.mainLoop()
          File "twisted/internet/base.py", line 1292, in mainLoop
            self.runUntilCurrent()
        --- <exception caught here> ---
          File "twisted/internet/base.py", line 913, in runUntilCurrent
            call.func(*call.args, **call.kw)
          File "/tmp/demo.py", line 11, in error_test
            int("Hello")
        exceptions.ValueError: invalid literal for int() with base 10: 'Hello'

2020-10-18T10:43:17-0400 [-] Received SIGINT, shutting down.
2020-10-18T10:43:17-0400 [-] Main loop terminated.
$python/tmp/demo.py 2>/tmp/demo err
^C
$cat/tmp/演示错误
2020-10-18T10:43:15-0400[-]未处理的错误
回溯(最近一次呼叫最后一次):
文件“/tmp/demo.py”,第15行,在
反应堆运行()
文件“twisted/internet/base.py”,第1283行,运行中
self.mainLoop()
文件“twisted/internet/base.py”,第1292行,在mainLoop中
self.rununtlcurrent()
---  ---
文件“twisted/internet/base.py”,第913行,在rununtlcurrent中
call.func(*call.args,**call.kw)
文件“/tmp/demo.py”,第11行,在错误测试中
int(“你好”)
exceptions.ValueError:以10为基数的int()的文本无效:“Hello”
2020-10-18T10:43:17-0400[-]收到信号,正在关闭。
2020-10-18T10:43:17-0400[-]主回路终止。

您可以通过

了解有关twisted.logger的更多信息。您确定它没有写入文件吗?我试着运行你的代码,效果很好。请记住,Python在文件运行时会对其进行锁定,因此它会在文件终止后保存并关闭文件。我尝试删除该文件,让程序创建一个新文件,因此我知道该文件是新的且未锁定。仍然没有写任何东西给它。我使用的是Windows10版本1909、Python 3.6和最新的Twisted。(可能应该提到)