【发布时间】:2015-05-12 23:25:48
【问题描述】:
我正在尝试将来自一个非常简单的烧瓶应用程序的应用程序日志消息保存在日志文件中。虽然这在我使用嵌入式 Flask 服务器运行应用程序时完美运行,但在 gUnicorn 中运行时它根本不起作用,基本上,没有应用程序输出被重定向,既不是日志文件(我的 Flask 应用程序中指定的文件)也不是运行 gunicorn 时的 STDOUT。
也就是说,这是我的 Flask 应用:
@app.route('/')
def index():
app.logger.debug('Into /!!!!')
print 'Will this print?'
return 'Flask is running!'
if __name__ == '__main__':
#Setup the logger
file_handler = FileHandler('test.log')
handler = logging.StreamHandler()
file_handler.setLevel(logging.DEBUG)
handler.setLevel(logging.DEBUG)
file_handler.setFormatter(Formatter(
'%(asctime)s %(levelname)s: %(message)s '
'[in %(pathname)s:%(lineno)d]'))
handler.setFormatter(Formatter(
'%(asctime)s %(levelname)s: %(message)s '
'[in %(pathname)s:%(lineno)d]'))
app.logger.addHandler(handler)
app.logger.addHandler(file_handler)
app.run(debug=True)
现在,如果我以以下方式启动应用程序:
python app.py
我得到了预期的输出:
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Restarting with stat
--------------------------------------------------------------------------------
DEBUG in app [app.py:23]:
Into /!!!!
--------------------------------------------------------------------------------
2015-03-11 09:36:18,375 DEBUG: Into /!!!! [in app.py:23]
Will this print?
127.0.0.1 - - [11/Mar/2015 09:36:18] "GET / HTTP/1.1" 200 -
拖尾test.log,我明白了:
2015-03-11 09:36:18,375 DEBUG: Into /!!!! [in app.py:23]
到目前为止一切看起来都很棒,然后当我尝试使用 nginx + gunicorn 运行应用程序时,首先我尝试像这样运行 gunicorn:
gunicorn app:app -b localhost:8000 --debug --log-level debug
应用程序正在运行,如果我去http://localhost:
curl http://localhost
Flask is running!
但是查看日志文件,是空的,没有写入任何内容。我添加了 777 权限只是为了检查它是否是权限问题无济于事。然后查看 gunicorn 标准输出,除了 print 语句之外什么都没有写:
2015-03-11 09:42:06 [25641] [DEBUG] GET /
Will this print?
Looking around,我尝试将所有输出重定向到 gunicorn 日志,然后像这样启动 gunicorn:
gunicorn app:app -b localhost:8000 --debug --log-file /tmp/test.log --log-level debug --error-logfile /tmp/error.log
但是现在我什至没有在 gunicorn 文件中获得打印语句,这是 test.log 和 error.log 的输出(它们是相同的):
2015-03-11 09:46:17 [26257] [DEBUG] tmp_upload_dir: None
2015-03-11 09:46:17 [26257] [DEBUG] keyfile: None
2015-03-11 09:46:17 [26257] [DEBUG] backlog: 2048
2015-03-11 09:46:17 [26257] [DEBUG] logger_class: simple
2015-03-11 09:46:17 [26257] [INFO] Starting gunicorn 17.5
2015-03-11 09:46:17 [26257] [DEBUG] Arbiter booted
2015-03-11 09:46:17 [26257] [INFO] Listening at: http://127.0.0.1:8000 (26257)
2015-03-11 09:46:17 [26257] [INFO] Using worker: sync
2015-03-11 09:46:17 [26262] [INFO] Booting worker with pid: 26262
2015-03-11 09:48:15 [26262] [DEBUG] GET /
有一个非常相似的问题here,其中一个答案似乎表明在gunicorn 中运行时没有可用的应用程序记录器???这听起来,至少,很奇怪......那我应该如何登录?
另一个proposed solution 似乎建议不要使用 Flask 记录器,但与 gunicorn 无关(我认为)...
我错过了什么?我应该放弃 gunicorn 并选择 Apache-mod wsgi 吗? Nginx-uWSGI?快速CGI?有什么想法吗?
谢谢! 亚历杭德罗
编辑:
我已经尝试过使用 uWGSI 而不是 gunicorn 和相同行为的相同设置,但没有获得任何应用程序日志记录。
现在基于 this response 和 other one,我想出了这个(在 gUnicorn 和 uWSGI 上,两者都有效)
from flask import Flask
import logging
from logging import Formatter, FileHandler
app = Flask(__name__)
LOGGER = logging.getLogger('whatever')
file_handler = FileHandler('test.log')
handler = logging.StreamHandler()
file_handler.setFormatter(Formatter(
'%(asctime)s %(levelname)s: %(message)s '
'[in %(pathname)s:%(lineno)d]'
))
handler.setFormatter(Formatter(
'%(asctime)s %(levelname)s: %(message)s '
'[in %(pathname)s:%(lineno)d]'
))
LOGGER.addHandler(file_handler)
LOGGER.addHandler(handler)
LOGGER.setLevel(logging.INFO)
@app.route('/')
def hello():
LOGGER.info('info log')
LOGGER.debug('debug log')
return 'Hello!'
if __name__ == '__main__':
app.run()
gunicorn 的输出:
2015-03-11 12:25:01 [11540] [INFO] Starting gunicorn 17.5
2015-03-11 12:25:01 [11540] [INFO] Listening at: http://127.0.0.1:8000 (11540)
2015-03-11 12:25:01 [11540] [INFO] Using worker: sync
2015-03-11 12:25:01 [11545] [INFO] Booting worker with pid: 11545
2015-03-11 12:26:20,765 INFO: info log [in /home/mosquito/www/flask-project/flask-project/app.py:24]
然后查看我的 test.log 文件:
2015-03-11 12:26:20,765 INFO: info log [in /home/mosquito/www/flask-project/flask-project/app.py:24]
所以是的,它有点工作,但最初的问题仍然存在......为什么 Flask 记录器在 wsgi 容器中运行时似乎不起作用 - gunicorn,uWSGI?
【问题讨论】:
-
这对我也有用。
标签: python logging flask gunicorn