本文介绍了Flask不打印sys.stdout.write消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过链接使用命令python app.py.

I am running the flask app from this link using the command python app.py.

使用sys.stdout.write打印的更多日志未打印在控制台上.但是,使用logging.StreamHandler可以将邮件重定向到stdout.

More logs printed using, sys.stdout.write are not printed on the console. However using logging.StreamHandler works for redirecting messages to stdout.

有效,

import logging
import logging.handlers
logger = logging.getLogger('kumologging')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler()
logger.addHandler(ch)
logger.info("hey, i am from logger obj")

它不起作用

import sys
sys.stdout.write("hey, i am from stdout")

flask是否会覆盖sys.stdout文件描述符,将日志重定向到其他位置?

Does flask override sys.stdout file descriptor, redirecting the logs elsewhere?

推荐答案

afaik烧瓶app.run()使用gunicorn网络软件包

afaik flasks app.run() uses the gunicorn web package

我相信gunicorn实际上是重定向您的输出的

I believe gunicorn is the one that is actually redirecting your output

输出可能在/var/log/gunicorn/error.log中(不确定在Windows:/上的位置)

the output might be in /var/log/gunicorn/error.log (not sure where it would be on windows :/)

或其可能的sys.stdout只是不刷新其缓冲区,写入后尝试sys.stdout.flush()

or its possible sys.stdout is just not flushing its buffer,try sys.stdout.flush() after writting to it

这篇关于Flask不打印sys.stdout.write消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 23:54