I am trying to diagnose some mysterious Internal Server Errors, so I added logging to the flask app. After digging through many explanations, I found this answer which I am using below, along with the suggestion to put everything inside a #app.before_first_request decorator.
Locally this works as expected and werkzeuglog.log contains exactly what is printed to the screen.
But on the server:
The log file is created.
Nothing is written to it.
No errors are thrown, at least not to the user
#app.before_first_request
def initialize():
logger = logging.getLogger('werkzeug')
logger.setLevel(logging.DEBUG)
handler = logging.FileHandler('werkzeuglog.log')
logger.addHandler(handler)
if __name__ == '__main__':
#run app
app.run(
host="0.0.0.0",
port=int("80"),
debug=False
)
Am I doing something wrong or is this some server issue that is beyond my scope? Gentle answers appreciated!
EDIT:
Some context: An internal server error is caused when the user uploads a file and I am trying to figure out what's going wrong using the log.
I don't think it can be an issue with permissions since the script definitely creates the file. I checked that the disk is not full.
EDIT 2
I reran the run.fcgi file which starts the WSGI Server. Now the manual entries to the log file via logger.info('blah blah') show up but not the others i.e. GET POST etc.
I don't have access to /var/log/nginx. It's a student-volunteer run server so I would like to give them as much info as I can. But this is all I have so far.
Related
I am attempting to teach myself flask from Medium and YouTube tutorials, and keep running into the same error.
Screenshot of 404 Not Found
Normally, I would assume I messed up, made a spelling mistake, and shouldn't embarrass myself by asking how to fix it on a public forum, but I literally copy-pasted the code without making any changes.
I found a Medium post that walks you through step-by-step with simple instructions and ran the code
from flask import Flask # import flask
app = Flask(__name__) # create an app instance
#app.route("/") # at the end point /
def hello(): # call method hello
return "Hello World!" # which returns "hello world"
if __name__ == "__main__": # on running python app.py
app.run() # run the flask app
and continue to get the same error. Does anyone know what I'm doing wrong?
Screenshot of Pycharm
Can you try changing app.run() to app.run(debug=True) and then restart the server?
Also, after restarting try visiting the link in incognito just to make sure it's not a caching issue.
As Medium explains further on in that tutorial:
For development purposes, we use something called as debug mode. When debug=True is set the server restarts as we add new code to our Flask Application.
So, I tried all of these and nothing worked. Then I restarted the computer and it still didn't work. And then I restarted is 2 more times and it worked, so I don't know what happened.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I built a little web app in Flask and am trying to deploy it on Pythonanywhere.
During development, I used a local server and launch my app as such:
if __name__ == '__main__':
app.run(debug=True,extra_files=['./static/results.csv',])
results.csv serves as a tiny database (I don't need more) and everytime a line is added to the file, from a Flask form submition, the app is releaded. I then get:
Detected change in '/Users/rodolphegonzales/sync/Survey/app/static/results.csv', reloading
Once I deploy on Pythonanywhere, this doesn't work anymore. when I run:
if __name__ == '__main__':
app.run(debug=False,extra_files=['./static/results.csv',])
I get:
Internal Server Error. The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Looking at the log, this is due to the fact that the changes in results.csv weren't detected, and that the app wasn't reloaded.
How to properly detect changes in extra files when deploying a Flask app?
EDIT: file change detection and reload is apparently impossible to do outside of the debug mode. A touch to the WSGI should automatically reload, I'll try it out.
This has nothing to do with the reloader (well, at least not the way you think). PythonAnywhere runs a WSGI server for you, pointed at your app. If you call the dev server unconditionally, it will block and PythonAnywhere's server will never pick it up.
Move the call to the dev server inside a __name__ guard.
if __name__ == '__main__':
app.run(...)
You should not reload an app automatically in production (and based on the way PythonAnywhere works, you wouldn't be able to, you need to manually reload). Based on what you've described, you shouldn't need to reload it at all, just re-read the static file when you need to get data. Given that most WSGI servers run in multiple processes, you're probably at risk of corrupting your simple file by writing to it from multiple processes at once. This is why you use a database.
Ok, my little knowledge of how this works in deployment confused me. it turns out I can't, outside of development using debug = True, and to the best of my knowledge, automatically observe changed files and reload the app through an argument in app.run(...). A solution is to "touch" Pythonanywhere's wsgi file anytime another form is submitted (in which case the app is reloaded). I'm doing it like this:
import os
os.utime('/var/www/USERNAME_pythonanywhere_com_wsgi.py', None)
This seems to do the trick. Thanks for your help!
I'm developing a Flask website using Visual Studio 2013's PythonTools, which has its own debugger, and that allows me to step through the initial setup code, up until app.run()
However the code I want to debug is the routing code, like this:
#app.route('/')
def url_index():
return render_template('index.html')
I know that function is running because the server does respond with index.html, but if I put a breakpoint on the last line, it'll never get hit.
Is there any way to debug these routing functions? Flask says it comes with a debugger but how do I use it? Will it be compatible with Visual Studio?
For the Flask debugger, you can set app.debug to True:
app.debug = True
or
app.run(debug=True)
And then:
#app.route('/')
def index():
raise
return render_template('index.html')
And then you can debug the function with the Flask debugger in your browser.
6 months later, and while it still doesn't look possible to automatically debug URL routing in flask, you can manually attach a debugger to the flask process, though you'll have to re-add it if you restart the server or if the auto-reloader detects changes in your .py files and restarts.
Just go:
Tools -> Attach to Process
and select the Python.exe that is not greyed out (that's the initial flask code that visual studio is already debugging), and then do something that would cause the breakpoint to be hit (e.g. reload the page), and you should have success.
Sadly the current version of PTVS doesn't support Flask projects.
Good thing is: the already released PTVS 2.1 alpha does: http://pytools.codeplex.com/wikipage?title=Flask
You can turn off reloading with debug mode by using
app.run(debug=True, use_reloader=False)
The Flask error handling docs go into the details of the debugging options.
I'm working on a website that uses Python web.py. There is a form where the user enters input and when the submit button is hit, a python page is called (matches) using the .getJSON JQuery function show below.
function buildMatches(input){
$.getJSON("/matches", {e:input}, function(json){
//Returned JSON object is worked on
}
}
The "matches" python page grabs a value from a DB, runs some string reg ex and returns a JSON object. Everything works fine, my question is how would I be able to output something from the python page "matches" to see what is exactly happening during the reg ex operations? I've tried print "" (python 2.5), but I understand that would print to the console. I've done some research but couldn't find anything for my situation. I don't necessarily need to print it out to the HTML page, just any where where I can see what's going on. Thanks in advance for any help.
I have access to the webserver (SSH, SFTP, etc.), I tried to log by importing the logging module, below is the code I used. I could get it to log if I ran the page from the command line, but not when it is called by the JS page.
import logging
logging.basicConfig(filename='./SomeClass.log', filemode='w', level=logging.DEBUG)
class SomeClass:
logging.info('Started')
logging.info('Another log')
def __init__(self, obj):
logging.info('In the init')
def another_functio(self):
logging.info('Logging inside the function')
I've tried setting the full path of the log and I still have the same problem where the log file will only be written or updated when I run this class from the console. This doesn't work when the class is called by the webserver.
logging.basicConfig(filename='/home/.../.../.../example.log', filemode='w', level=logging.DEBUG)
Depending on how much access you have to the web server you can run your code manually so web.py uses its built-in web server. Then print statements will end up on the console.
Otherwise, have you thought about simply writing to your own log file somewhere accessible with a browser?
Thanks again for all the help. After digging more into the setup of the Apache server and Python implementation I was able to find a solution to help me see what's going and debug my web app. I saw that Apache config is setup to log errors and WSGI also blocks (pukes on) std.out. What I was able to do is redirect the print command to the Apache error log files.
print >> sys.stderr, "Debugging print with variable" + variable
Then I check the Apache error log to start debugging the web app. I thought I would share this in case anyone else ran into this problem as it was a pain for me.
Thanks again.
I'm running my app on the GAE development server, with app-engine-patch to run Django.
One of my views is bugged , so I want to log everything that happens.
I added in myapp.views:
import logging
LOG_FILENAME = '/mylog.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
and my function is:
def function(string):
logging.debug('new call')
#do stuff
#logging.debug('log stuff')
My problem is that I can't find the log. When I run my app I get no errors, but the log is not created.
I also tried various paths: /mylog.txt ,mylog.txt , c:\mylog.txt, c:\complete\path\to \my\app\mylog.txt, but it doesn't work.
On the other hand I tried to create and run a separate test:
#test.py
import logging
LOG_FILENAME = '/mylog.txt'
logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG)
logging.debug('test')
And the log is created without problems: c:\mylog.txt
I'm not familiar with logging so I don't know if there might be some issues with django, or appengine.
Thanks
You can't write to files on App Engine - thus, any attempt to log to text files is also doomed to failure. Log output will appear on the SDK console in development, or in the logs console in production.
I am guessing the problem is that you put your log configuration in a view. A general rule of thumb for django logging is to set the log configuration in your settings.py.
By default, dev_appserver suppresses the debug log. To turn it on, run it with the --debug option. Note that dev_appserver itself uses the same logger, and will spew lots of debugging stuff you might not care about.
Your logging will print to stderr.