簡體   English   中英

python日志記錄顯示來自不同線程的sys.stdout

[英]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使用WorkerThreadThreadPool來實現並發例證這里

你必須顯示更多代碼。 您在其中顯示的代碼, stream_handler被創建,但隨后handler是使用addHandler

我的猜測是你的日志記錄級別設置不正確,導致來自WorkingThread1所有日志都沒有被記錄。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM