Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logging flask errors with mod_wsgi

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.

like image 995
Prakhar Avatar asked Nov 04 '12 09:11

Prakhar


2 Answers

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.

like image 135
Graham Dumpleton Avatar answered Sep 19 '22 18:09

Graham Dumpleton


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')
like image 40
Martijn Pieters Avatar answered Sep 21 '22 18:09

Martijn Pieters