简体   繁体   English

python日志记录显示来自不同线程的sys.stdout

[英]python logging shows no sys.stdout when from different thread

i now have a strange problem with logging in my multithreaded python application. 我现在有一个奇怪的问题,登录我的多线程python应用程序。 Whenever i debug the application, i properly see the logging output in the stdout, such as 每当我调试应用程序时,我都能正确地看到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/. 

However, when i run the code without debug, all the logs from the WorkingThread1 disappear, leaving only the MainThread ones. 但是,当我在没有调试的情况下运行代码时, WorkingThread1所有日志都会消失,只留下MainThread的日志。 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. WorkerThread1是从pyTelegramBotAPI框架启动的。 I have my logs output to the sys.stdout : 我有我的日志输出到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. 更新 :它有100%与多线程有关,因为当我告诉框架只使用一个线程时,会出现日志消息。 pyTelegramBotAPI uses WorkerThread and ThreadPool to implement concurrency as exemplified here pyTelegramBotAPI使用WorkerThreadThreadPool来实现并发例证这里

You have to show more code. 你必须显示更多代码。 In the code which you have shown, stream_handler is created, but then handler is used in addHandler . 您在其中显示的代码, stream_handler被创建,但随后handler是使用addHandler

My guess will be that your logging level is improperly set causing all the logs from the WorkingThread1 to be not logged. 我的猜测是你的日志记录级别设置不正确,导致来自WorkingThread1所有日志都没有被记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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