本文介绍了在gunicorn内运行时,Flask应用程序记录器不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个非常简单的应用程序日志文件中保存应用程序日志消息。虽然这运作完美,当我运行与嵌入式Flask服务器的应用程序时,它在gUnicorn中运行时根本不工作,基本上,没有应用程序输出重定向日志文件(在我的Flask应用程序中指定的)或运行gunicorn时的标准输出。

也就是说,这是我的Flask应用程序:

  @app ('/')
def index():
app.logger.debug('Into / !!!!')
print'这个打印吗?'
'

$ b如果__name__ =='__main__':
#设置记录器
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'
'[%(pathname)s:%(lineno)d]'))
handler.setFormatter(Formatter(
'%(asctime)s%(levelname)s:%(message)s'
'[%(pathname)s:%(lineno)d]'))
app.logger。 addHandler(处理程序)
app.logger.addHandler(file_handler)
app.run(debug = True)

现在,如果我启动的应用程序为:

  python app.py 

我得到了预期的输出:

 *运行于http://127.0.0.1:5000/(按CTRL + C退出)
*用stat

---- -------------------------------------------------- --------------------------
DEBUG在应用程序[app.py:23]:
进入/ !!! !
---------------------------------------------- ----------------------------------
2015-03-11 09:36:18,375 DEBUG:进/ !!!! [在app.py:23]
这个打印?
127.0.0.1 - - [11 / Mar / 2015 09:36:18]GET / HTTP / 1.1200 -

Tailing test.log,我看到:

  2015-03-11 09:36: 18,375 DEBUG:进/ !!!! [app.py:23] 

目前为止,应用程序与nginx + gunicorn,首先我试图运行gunicorn是这样的:

  gunicorn应用程序:应用程序-b本地主机:8000 --debug --log级别调试

如果我去:
$ b $

  curl http :// localhost 
Flask正在运行!

但是查看日志文件是空的,没有任何内容正在写入。我已经添加了777个权限,只是为了检查这是否是一个权限问题无济于事。然后看看gunicorn stdout,除了print声明之外,没有什么可写的:

  2015-03-11 09:42:06 [ 25641] [DEBUG] GET / 
这个打印?

,其中一个答案似乎表明,没有应用程序记录器是可用时在gunicorn内运行?这听起来,至少,很奇怪...我应该如何登录?

另外似乎不建议使用Flask记录器,但与gunicorn不相关)...



我错过了什么?我应该放弃gunicorn,去Apache-mod的wsgi? Nginx的-uWSGI? FastCGI的?任何想法?



谢谢!
亚历杭德罗



编辑

与uWGSI相同的设置,而不是gunicorn和相同的行为,没有获得任何应用程序日志记录。

现在基于,并且这个,我想出了这个(在gnornorn和uWSGI,在这两个作品)

  from flask导入Flask 
导入日志记录
从日志导入Formatter,FileHandler
$ b $ 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.setFormatt er(格式化程序(
'%(asctime)s%(levelname)s:%(message)s'
'[%(pathname)s:%(lineno)d]'
))
LOGGER.addHandler(file_handler)
LOGGER.addHandler(处理程序)
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]启动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 ]使用worker:sync
2015-03-11 12:25:01 [11545] [INFO]用pid启动worker: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? / a>为WSGI。您看到的Flask日志实际上来自服务器,而不是来自Flask本身。



当您用Gunicorn或uWSGI替换该开发服务器时,您不会看到它的日志。

调试器也一样。即使只使用,也可以看到熟悉的Flask调试页面 。



现在你知道了。 :)

I'm trying to save application log messages from a very simple flask app in a log file. While this works flawlessly when I'm running the app with the embedded Flask server, it is not working at all when running within gUnicorn, basically, no application output is redirected neither the log file (the one specified in my Flask app) or to the STDOUT when running gunicorn.

That said, this is my Flask app:

@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)

Now if I start the app as:

python app.py

I get the expected output:

 * 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 -

Tailing test.log, I see:

2015-03-11 09:36:18,375 DEBUG: Into /!!!! [in app.py:23]

Everything looks great so far, then when I try to run the app with nginx + gunicorn, first I've tried to run gunicorn like this:

gunicorn app:app -b localhost:8000 --debug --log-level debug

App is working, if I go to http://localhost:

curl http://localhost
Flask is running!

But looking at the log file, is empty, nothing is being written. I've added 777 permissions just to check if it was a permission problem to no avail. Then looking at gunicorn stdout, nothing is being written besides the print statement:

2015-03-11 09:42:06 [25641] [DEBUG] GET /
Will this print?

Looking around, I've tried to redirect all output to gunicorn logs, then starting gunicorn like this:

gunicorn app:app -b localhost:8000 --debug --log-file /tmp/test.log --log-level debug --error-logfile /tmp/error.log

But now I don't even get print statements in gunicorn files, this is the output from both test.log and error.log (they are identical):

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 /

There is a very similar question here, one of the answers seems to suggest that no application logger is available when runnning within gunicorn??? This sounds, at least, quite strange...how am I supposed to log then?

Another proposed solution seems to suggest not using Flask logger, but is not related with gunicorn (I think)...

What I'm missing? Should I give up on gunicorn and go for Apache-mod wsgi? Nginx-uWSGI? FastCGI? Any ideas?

Thanks!Alejandro

EDIT:

I've tried this very same setup with uWGSI instead of gunicorn and same behaviour, no application logging is obtained whatsoever.

Now based on this response and this other one, I came up with this (on gUnicorn and uWSGI, in both it works)

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()

Output from 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]

And looking at my test.log file:

2015-03-11 12:26:20,765 INFO: info log [in /home/mosquito/www/flask-project/flask-project/app.py:24]

So yeah, it kinda works, but original question still remains...why the heck the Flask logger does not seem to work when running inside wsgi containers - gunicorn, uWSGI?

解决方案

Flask uses Werkzeug for WSGI. The "Flask logs" you see are actually from Werkzeug's builtin development server and not from Flask itself.

When you replace that development server with something like Gunicorn or uWSGI, you don't see its logs.

The same goes for the Debugger. You can see the familiar "Flask debug page" even if you only use Werkzeug's Debugger.

Now you know. :)

这篇关于在gunicorn内运行时,Flask应用程序记录器不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-28 01:45