参考https://www.icode9.com/content-1-335444.html
Python3默认的print方法会存在缓存,在生产环境运行尤其使用Supervisor运行flask不会输出,除非每个print增加参数flush=True来刷新缓存【实时输出】或直接用logging模块方法
问题:配置supervisor + gunicorn 不实时刷新print日志,使用python 直接运行 或直接 gunicorn 直接运行日志没问题
supervisor + gunicorn 只有当 重启supervisor时才能看到输出,不过启动supervisor 后 运行一段时间,也会刷新日志!
出现以上问题的根据就是Python的print方法存在缓存,要么增加参数flush=Tru开启刷新缓存,要么使用logging.warning方法【更加推荐logging.warning方法,logging默认基本为warning】
如果你要使用debug或info,则必须修改logging默认级别【建议输出直接使用warning即可】
python 日志logging级别及使用参考:https://www.cnblogs.com/qianxunman/p/12172994.html
python 日志 logging模块(详细解析):https://blog.csdn.net/pansaky/article/details/90710751
(python 3)
# 【方式一】开始刷新缓存
print(something, flush=True)
# 【方式二,推荐使用】logging必须使用warning
import logging
logging.warning('Watch out!')