Reputation: 546
I deployed a Flask app on my Apache 2.2 instance on Windows, everything running fine.
Then I decided to implement logging in my app and read the Flask docs: I decided to go with a file logger, which generates a logfile containing all the logs messages that I expect...this happens when I execute the application using the builtin Flask webserver. Here is the relevant code:
if __name__ == '__main__':
#Create a rotating logfile for the application
handler = RotatingFileHandler('myapp.log', maxBytes=10000, backupCount=1)
handler.setLevel(logging.INFO)
handler.setFormatter(Formatter('[%(levelname)s][%(asctime)s] %(message)s'))
app.logger.setLevel(logging.INFO)
app.logger.addHandler(handler)
#Run the app
app.run(host='0.0.0.0', port=5000)
But when I deploy the app to Apache, my app doesn't create/write that logfile. Log messages are not even written in standard Apache logfiles, as one could expect because Apache knows nothing about my logging framework.
Is there a way to force my app create the logfile also when executed via Apache? It would also be acceptable to have Apache write my application logs to another file determined via the its configuration.
Thanks in advance!
Upvotes: 2
Views: 2194
Reputation: 8244
You have the whole logging setup code inside if __name__ == '__main__'
, which only runs if you use the development server of Flask. Which is obviously not the case on your Apache server. The short answer is:
Move the whole logging code before the if-condition and you should be fine.
You could also put the code in an else
block after the if-block, to disable logging when developing (since you're using debug mode there anyway). But actually i would put server-specific stuff in server-specific files that are not part of the app.
So you have a production "launcher":
from myapp import app
# logging stuff
# app.logger.setHandler(...)
# more configuration
# do some stuff for apache (?)
And a debugging "launcher":
from myapp import app
app.run(debug=True)
Upvotes: 4
Reputation: 21
Since you are running Windows:
Which user account runs the Apache service? Default it's System. This account is limited in accessing network resources. Is your Flask application located on a shared network drive?
Did you try to set a full path to your log file?
If this doesn't help, can you post the Apache configurations of your application?
Edit:
I just saw that you configure the logging under __name__ == '__main__'
If i'm not mistaken this code is not executed when the app is deployed with mod_wsgi. In this case you'll have to configure your logging somewhere else.
Upvotes: 2
Reputation: 25
Make sure that the user that runs the apache process has permissions on the folder you are trying to write to. Also, I would check the result of:
sudo find / -name myapp.log
Upvotes: 0