Logging standard out with django and nginx - django

I am trying to debug an application I made with django and it works fine using django's manage.py, but when I use nginx some features do not work. I checked the error logs and they are empty, probably because I am catching all exceptions and sending them to standard out. Is there an easy way to see the output from the application with nginx?

WSGI servers often disable stdout, I think because it can cause some sort of compatibility problems. Django has built-in logging; I'd suggest using that instead of print statements. It should both be easier to configure and also allow you to have more specificity in your logging, i.e. :
log.debug('only seen while debugging')
or
log.warn('this message might indicate a problem')
Relevant django docs:
https://docs.djangoproject.com/en/dev/topics/logging/

Related

Django. How to debug a deployed website without turning on Debug setting

I have a little forum that is already deployed and working. I just found out something is wrong with the Twitter login and I need to debug it. Right now it returns a 500 page. I can't know what is wrong with Debug = True. But if I turn it on, then I'll be violating Django security best practices, and if someone saves the info in the debug page (with Debug=False) and has bad intentions, he could cause lots of trouble. What would you recommend?
Make sure you have access to the application logs and have properly configured the AdminEmailHandler.
Make sure you've property configured ALLOWED_HOSTS. If you haven't configured it and set DEBUG=False you'll see a SuspiciousOperation raised.
Double check that you have the incidentals covered. Bad database connection details will quickly cause 500 errors.
Have you tried running it locally with DEBUG=False? That's often a quick way to find out about some more esoteric errors.
To follow up based on the comments you'll need to turn DEBUG on in a somewhat live environment. The standard way to do this would be to have a separate staging environment from the live one. Since Heroku is being used you can easily spin up a separate environment and set DEBUG=True there. Then you'll see the full error pages, fix the error, and deploy to production where DEBUG=False.
Another idea is to setup a third-party exception handling system like Raygun or Bugsnag. Adding this to the production application will give you reports when exceptions are thrown. This is a big upgrade over Django's default email-on-error behavior.

What is an efficient way to view errors in Django?

I am trying to add a new middle ware into my Django application. However, my app now stalls when being visited. Eventually, after a long time, a 500 error surfaces.
Is there an error log anywhere on the server for Django? I want to see what the error was.
In general, logging will depend on how you are serving django (modwsgi, gunicorn, passenger, etc...), how you have configured django logging and where the error occurs, i.e. if the error happens before/after django is handling the request.
If you have configured your mail host and logging, django can send you the error messages. If the error occurs and django isn't handling it, it will generally show in your server's logs.
Personally, I think one of the best ways to view django errors is using raven with sentry, either self hosted or on getsentry. It even comes with a wsgi middleware to catch errors on the wsgi layer.
It looks like you get a timeout error, you can look at your webserver logs but it will probably show nothing interesting, I suppose you are doing something too heavy in your new middleware that takes too long and then times out the request.
I really suggest you to re-check your middleware code (maybe you can add the code here)

My Apache (mod_wsgi) Django app only lists the files instead of the actually running website/application

My Apache (mod_wsgi) Django app only lists the files of my website/project instead of the actual running website/application. My guess is that it's mod_wsgi that is the culprit but I'm not really sure. What are some of the causes of this?
Don't put your Django site code in a directory Apache can serve up. Right now if I knew the actual site host name, I could get down your Django settings file and get access to your database password information.
Anyway, ensure you have gone and set up a basic WSGI hello world program before you attempt to get Django working under mod_wsgi. In doing this, ensure you use the official mod_wsgi documentation and not some arbitrary persons blog post. As such, go read:
http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
Also go watch the video presentation at:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Conference_Presentations
as it steps you through basic configuration as well as what to do for Django. It covers all the basic things that people do wrong.
If you still can't get it to work, then heed the comments in:
http://code.google.com/p/modwsgi/wiki/WhereToGetHelp?tm=6#Asking_Your_Questions
about useful information you should supply to any forum to help people solve your problem.

Logging Django Errors instead of returning them to inspect API callback

I'm working on a project that gets callbacks from some other sites' API. I expect my code to have a few errors, because I'm new to Python & Django. My site in development is using mod_wsgi and Apache with Debug = True ;)
The API I get the calls from adds a parameter to the querystring to my callback that is built using their own private key. So I have no way of accurately simulating that. If my code fails once I activate my probably faulty key validation code, I have no way of knowing except for apache server logs (which don't show the actual stacktrace or anything).
How can I log a more detailed python exception, like the one I usually see in my browser every 2 minutes ;) to a file? Especially the local variables around the faulty line are interesting obviously :)
Thanks! :)
If you set ADMINS in your settings.py (and set DEBUG=False) you will be emailed all 500 server errors (just like the DEBUG error page).
However, if you want your app to continue without responding with a 500, you can import logging and write your own debug log. There's a good tutorial here:
http://simonwillison.net/2008/May/22/debugging/

Using nginx/fcgi/django, I have form posts that give a 504 gateway time-out

I have an app that uses Django with FCGI on nginx. I'm using the third-party apps like James Bennett's django-registration and django-messages from the Pinax Project. Both of these apps have forms that are submitted and save data into the database, then redirect on to a new URL.
My issue seems to be that the .save() method on any of the forms in these apps cause a 504 gateway time-out when the forms are submitted. All of the data is saved in the database as it should be, however neither seems to ever return anything to the app after the form is saved.
I've done some logging at various points in the code and there are no errors. It's as if the save() methods on the form or the models the forms are connected to simply never return anything--error or otherwise.
With this lack of detail, an answer might be a dream for me, but just a nudge in the right direction or a way to diagnose the issue more completely would be fantastic.
Typically 504's in nginx happen due to timeout between nginx and the fastcgi process. You may want to take a look at your nginx settings and up the fastcgi_read_timeout setting?
Your nginx error log will typically provide a bit more information as to why things are not working as well. If you're on a *nix distro it's typically in "/var/log/nginx/error.log"
Turns out the problem was completely unrelated to nginx, but was Django having a timeout when trying to send an email. Unfortunately, it doesn't drop an error message or any indication that it can't connect to the email server.