Trying to log 500 errors to a log file on my Flask web app. Works properly on localhost, but not on the production server which uses mod_WSGI and Apache on Ubuntu (as per this setup @ https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps )
from flask import Flask, render_template
import logging
app = Flask(__name__)
app.config['LOG_FILE'] = 'application.log'
if not app.debug:
import logging
from logging import FileHandler
file_handler = FileHandler(app.config['LOG_FILE'])
file_handler.setLevel(logging.INFO)
app.logger.addHandler(file_handler)
@app.route("/about")
def about():
##CREATE AN ERROR
X = "A" + 1
return render_template('about.html')
if __name__ == "__main__":
app.run('0.0.0.0', port=8080, debug=False)
Any ideas to why this is working in the local environment but not in production? Changing debug setting doesn't seem to resolve.
What's most likely happening is that you are running your web server on development with user X, and this user has write permissions to the directory your Flask app is stored in - therefore it can write to application.log
.
I noticed from the tutorial you posted that all directores were created using sudo
and therefore your directory permissions are owned by the root user, while your Flask application is running as a different user that doesn't have permission to write to a directory owned by root.
There are two potential fixes:
Change the permissions of the directory where your Flask app is installed to be the same:
$ sudo chown -R www-data:www-data /var/www/FlaskApp
OR
Change where you write your log file (preferred method):
$ sudo mkdir /var/log/flaskapp
$ sudo chown -R www-data:www-data /var/log/flaskapp
and then change code to:
app.config['LOG_FILE'] = '/var/log/flaskapp/application.log'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.