logging flask errors with mod_wsgi

徘徊边缘 提交于 2021-01-27 04:09:11

问题


I've been trying to get this working since a long time but I'm really at my wits end now. I've tried to do everything that I could find on SO and flask documentation and still I cant a simple error log working so that I can debug my applcation. Below is the pasted code -

# main.py
from flask import Flask
import logging

app = Flask(__name__)
file_handler = logging.FileHandler(filename='/tmp/election_error.log')
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)

@app.route('/')
def hello():
   return "hello
   #missing quotes to generate error

if __name__ == "__main__":
   app.run()


#wsgi file
import sys
import logging
sys.path.insert(0, "/var/www/voting_app")
logging.basicConfig(stream=sys.stderr)
from main import app as application


# apache2 conf file
WSGIDaemonProcess voting_app threads=5
WSGIScriptAlias /election /var/www/voting_app/voting_app.wsgi

LogLevel info

<Directory /var/www/voting_app>
        WSGIProcessGroup voting_app
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
</Directory>

Please tell me where I'm going wrong. Thank you so much.


回答1:


The specific error you created, which was a syntax error, would have caused failure of the WSGI script file to even load in mod_wsgi. The error for this would have ended up in the Apache error log file, not the log file you are setup using the logging module. Have you looked in the Apache error log file?

For an exception raised during request execution, Flask would by default turn it into a 500 error page and otherwise supress the display of the details. You need to set up Flask to mail or log such runtime exceptions in other ways per:

http://flask.pocoo.org/docs/errorhandling/

If you want a runtime exception to be displayed in the 500 page returned to the browser for development purposes, you need to enable Flask debug mode. This is done by setting app.debug to be True:

http://flask.pocoo.org/docs/config/?highlight=app%20debug

You should not have debug mode enabled on a user facing production system.




回答2:


You'll need to generate a runtime exception, not a compile time exception. A missing quote is a compile time exception and your logging code will never be executed.

Just raise an exception instead:

@app.route('/')
def hello():
   raise Exception('Deliberate exception raised')


来源:https://stackoverflow.com/questions/13217490/logging-flask-errors-with-mod-wsgi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!