[Fixed]-Error Logging in Django and Gunicorn

3👍

tl;dr there’s nothing wrong with your code

It seems you’ve followed the linked tutorial correctly, and would probably find your log files in the /home/junsu/sites/superlists-staging.mysite.com/ dir.

Regardless, there are a few points to address in your question, I’ll try to do that.

Loggers and handlers

The settings module you reference above sets up a single logging handler console (StreamHandler), and a single django logger which can use that handler.

The root logger does not define any handlers, and “django” will log anything to stderr, and only for level INFO and above. I ran a quick test, and the root logger also has a StreamHandler defined by default.

Your authentication.py module currently calls logging.warning which logs to root logger (i.e it does logger = logging.getLogger(); logger.warning('stuff')). However, you may want to define a more specific handler to easier locate the log of your module. This is explained in this section of the referenced tutorial.

Gunicorn redirects stderr by default

It seems by default is set up to capture the stderr stream, which you currently redirect to a log file. However, my suggestion is to use your daemonizing app (seems like you’re using upstart) to log the stderr/out.

Upstart logging

As explained in gunicorn docs, configuring upstart is pretty simple.

If you remove the --error-logfile option in your /etc/init/gunicorn-superlists-staging.mysite.com.conf config, gunicorn will default to logging it’s output to stderr which can then be captured by upstart in whatever manner you prefer.

If you are using upstart 1.7 or greater, stdout/err capturing should be enabled by default. If, however, you use an earlier version of upstart, my suggestion is to add a console log option in your config and all output (stdout/stderr) will simply be logged to (I assume) /var/log/upstart/gunicorn-superlists-staging.mysite.com.log

15👍

For those searching for error logs for (nginx + gunicorn + django) setup:

Simply use these
(Some commands also specific to where your socket files are located)

  • Check the Nginx process logs by typing: sudo journalctl -u nginx
  • Check the Nginx access logs by typing: sudo less /var/log/nginx/access.log
  • Check the Nginx error logs by typing: sudo less /var/log/nginx/error.log
  • Check the Gunicorn application logs by typing: sudo journalctl -u gunicorn
  • Check the Gunicorn socket logs by typing: sudo journalctl -u gunicorn.socket

Reference

Leave a comment