[英]python logging shows no sys.stdout when from different thread
我现在有一个奇怪的问题,登录我的多线程python应用程序。 每当我调试应用程序时,我都能正确地看到stdout中的日志记录输出,例如
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/.
但是,当我在没有调试的情况下运行代码时, WorkingThread1
所有日志都会消失,只留下MainThread
的日志。 代码保持不变,错误仍然存在。 我想这与多线程有关。 WorkerThread1
是从pyTelegramBotAPI
框架启动的。 我有我的日志输出到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_handler
被创建,但随后handler
是使用addHandler
。
我的猜测是你的日志记录级别设置不正确,导致来自WorkingThread1
所有日志都没有被记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.