40

I am currently developing an application based on flask. It runs fine spawning the server manually using app.run(). I've tried to run it through mod_wsgi now. Strangely, I get a 500 error, and nothing in the logs. I've investigated a bit and here are my findings.

  • Inserting a line like print >>sys.stderr, "hello" works as expected. The message shows up in the error log.
  • When calling a method without using a template it works just fine. No 500 Error.
  • Using a simple template works fine too.
  • BUT as soon as I trigger a database access inside the template (for example looping over a query) I get the error.

My gut tells me that it's SQLAlchemy which emits an error, and maybe some logging config causes the log to be discarded at some point in the application.

Additionally, for testing, I am using SQLite. This, as far as I can recall, can only be accessed from one thread. So if mod_wsgi spawns more threads, it may break the app.

I am a bit at a loss, because it only breaks running behind mod_wsgi, which also seems to swallow my errors. What can I do to make the errors bubble up into the apache error_log?

For reference, the code can be seen on this github permalink.

exhuma
  • 20,071
  • 12
  • 90
  • 123

3 Answers3

54

Turns out I was not completely wrong. The exception was indeed thrown by sqlalchemy. And as it's streamed to stdout by default, mod_wsgi silently ignored it (as far as I can tell).

To answer my main question: How to see the errors produced by the WSGI app?

It's actually very simple. Redirect your logs to stderr. The only thing you need to do, is add the following to your WSGI script:

import logging, sys
logging.basicConfig(stream=sys.stderr)

Now, this is the most mundane logging config. As I haven't put anything into place yet for my application this will do. But, I guess, once the application matures you will have a more sophisticated logging config anyways, so this won't bite you.

But for quick and dirty debugging, this will do just fine.

exhuma
  • 20,071
  • 12
  • 90
  • 123
  • 6
    http://flask.pocoo.org/docs/errorhandling/ explains this as well. If debug mode is enabled, errors will flow to the apache log. Once you disable it, you need to have logging set up or they will go nowhere. I can confirm that `logging.handlers.SMTPHandler` works nicely for deployed applications. – robots.jpg Nov 04 '11 at 14:35
  • @robots.jpg: I missed that... Sometimes it's obviously best to read the docs several times... ;) – exhuma Nov 04 '11 at 16:58
  • I believe equivalently except that it's non-global: `app.logger.addHandler(logging.StreamHandler(stream=sys.stderr))` – jpmc26 Feb 26 '13 at 19:28
0

I had a similar problem: occasional "Internal Server Error" without logs. When you use mod_wsgi you should remove "app.run()" because this will always start a local WSGI server which we do not want if we deploy that application to mod_wsgi. See docs. I do not know if this is your case, but I hope this can help.

Daniel Lerch
  • 661
  • 7
  • 11
-2

If you put this into your config.py it will help dramatically in propagating errors up to the apache error log:

PROPAGATE_EXCEPTIONS = True
exhuma
  • 20,071
  • 12
  • 90
  • 123
Hidden Name
  • 354
  • 1
  • 3
  • 8