Log fake errors using mod_wsgi

I have been trying to get this job since ancient times, but now I'm really on my way. I tried to do everything I could find in the SO documentation and jars, and yet I cannot create a simple error log so that I can debug my application. Below is the code inserted -

# 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 am wrong. Thank you very much.

+6
source share
2 answers

The specific error you created, which was a syntax error, would cause the WSGI script file to even load into mod_wsgi. The error for this would end in the Apache error log file, and not in the log file that you configure using the log module. Have you looked at the Apache error log file?

For an exception that occurred during the execution of a request, Flask will turn it into a 500 error page by default and otherwise suppress the display of details. You need to configure a flag to send mail or register such exceptions at runtime in other ways:

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

If you want the runtime exception to appear on page 500 returned to the browser for development purposes, you need to enable debug mode in the jar. This is done by setting app.debug as True:

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

You should not have debug mode for the user facing the production system.

+5
source

You need to throw an exception at runtime, 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:

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

All Articles