python日志记录在来自不同线程时不显示sys.stdout
我现在在登录我的多线程python应用程序时遇到了一个奇怪的问题。每当调试应用程序时,我都会在标准输出中正确地看到日志输出,例如python日志记录在来自不同线程时不显示sys.stdout,python,multithreading,logging,telegram-bot,Python,Multithreading,Logging,Telegram Bot,我现在在登录我的多线程python应用程序时遇到了一个奇怪的问题。每当调试应用程序时,我都会在标准输出中正确地看到日志输出,例如 2016-11-05 21:51:36,851 (connectionpool.py:735 MainThread) INFO - requests.packages.urllib3.connectionpool: "Starting new HTTPS connection (1): api.telegram.org" 2016-11-05 21:51:41
2016-11-05 21:51:36,851 (connectionpool.py:735 MainThread) INFO - requests.packages.urllib3.connectionpool: "Starting new HTTPS connection (1): api.telegram.org"
2016-11-05 21:51:41,920 (converter.py:16 WorkerThread1) DEBUG - converter: "resizing file test/test_input/"
2016-11-05 21:51:50,199 (bot.py:221 WorkerThread1) ERROR - __main__: "MoviePy error: failed to read the duration of file test/test_input/.
但是,当我在没有调试的情况下运行代码时,来自工作线程read1
的所有日志都会消失,只剩下主线程的日志。代码保持不变,错误仍然存在。我想这与多线程有关。WorkerThread1
是从PyDatamebotAPI
框架启动的。我将日志输出到sys.stdout
:
formatter = logging.Formatter(
'%(asctime)s (%(filename)s:%(lineno)d %(threadName)s) %(levelname)s - %(name)s: "%(message)s"')
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setFormatter(formatter)
root = logging.getLogger()
root.setLevel(logging.NOTSET)
root.addHandler(stream_handler)
有什么想法吗
更新:它100%与多线程有关,因为当我告诉框架只使用一个线程时,会出现日志消息pyTelegramBotAPI
使用WorkerThread
和ThreadPool
实现并发,如示例所示您必须显示更多代码。在您显示的代码中,创建了stream\u handler
,但在addHandler
中使用了handler
我猜您的日志记录级别设置不正确,导致来自WorkingRead1
的所有日志未被记录。您必须显示更多代码。在您显示的代码中,创建了stream\u handler
,但在addHandler
中使用了handler
我猜您的日志记录级别设置不正确,导致来自工作区read1的所有日志未被记录。对不起,我已更正了代码<实际上使用了代码>流处理程序
。请参阅updatesorry,我已经更正了代码<实际上使用了代码>流处理程序
。请参阅updateUpdate:它100%与多线程有关,因为当我告诉框架仅使用一个线程时,日志消息会出现更新:它100%与多线程有关,因为当我告诉框架仅使用一个线程时,日志消息会出现