简体   繁体   中英

python logging shows no sys.stdout when from different thread

i now have a strange problem with logging in my multithreaded python application. Whenever i debug the application, i properly see the logging output in the stdout, such as

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/. 

However, when i run the code without debug, all the logs from the WorkingThread1 disappear, leaving only the MainThread ones. The code is unchanged and the error remains. I guess it has something to do with multithreading. The WorkerThread1 is started from the pyTelegramBotAPI framework. I have my logs output to the 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)

Any ideas?

Update : it has 100% to do with multithreading, because when i tell the framework to only use one thread, the logging messages appear. pyTelegramBotAPI uses WorkerThread and ThreadPool to implement concurrency as exemplified here

You have to show more code. In the code which you have shown, stream_handler is created, but then handler is used in addHandler .

My guess will be that your logging level is improperly set causing all the logs from the WorkingThread1 to be not logged.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM