Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error Logging in Django and Gunicorn

I want logging in Django and Gunicorn, when I get error. I study with TDD with Python, http://chimera.labs.oreilly.com/books/1234000000754/ch17.html#_setting_up_logging

this is my code. /etc/init/gunicorn-superlists-staging.mysite.com.conf

description "Gunicorn server for superlists-staging.mysite.com"

start on net-device-up
stop on shutdown

respawn

setuid junsu
chdir /home/junsu/sites/superlists-staging.mysite.com/source

exec ../virtualenv/bin/gunicorn \
    --bind unix:/tmp/superlists-staging.mysite.com.socket \
    --access-logfile ../access.log \
    --error-logfile ../error.log \
    superlists.wsgi:application

accounts/authentication.py

import requests
import logging
from django.contrib.auth import get_user_model
User = get_user_model()

PERSONA_VERIFY_URL = 'https://verifier.login.persona.org/verify'
DOMAIN = 'localhost'

class PersonaAuthenticationBackend(object):

    def authenticate(self, assertion):
        logging.warning('authenticate function')
        response = requests.post(
            PERSONA_VERIFY_URL,
            data={'assertion': assertion, 'audience': settings.DOMAIN}
        )
        logging.warning('got response form persona')
        logging.warning(response.content.decode())
        if response.ok and response.json()['status'] == 'okay':
            email = response.json()['email']
            try:
                return User.objects.get(email=email)
            except User.DoesNotExist:
                return User.objects.create(email=email)

    def get_user(self, email):
        try:
            return User.objects.get(email=email)
        except User.DoesNotExist:
            return None

superlists/settings.py

[....]
LOGGING = {
    'version': 1,
    'disable_existing_logger': False,
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
    },
    'loggers': {
        'django': {
            'handlers': ['console'],
        },
    },
    'root': {'level': 'INFO'},
}

my "error.log" just log this.

[2016-02-08 14:42:56 +0900] [3355] [INFO] Listening at: unix:/tmp/superlists-staging.mysite.com.socket (3355)
[2016-02-08 14:42:56 +0900] [3355] [INFO] Using worker: sync
[2016-02-08 14:42:56 +0900] [3359] [INFO] Booting worker with pid: 3359
[2016-02-08 14:58:22 +0900] [3355] [INFO] Handling signal: term
[2016-02-08 14:58:22 +0900] [3355] [INFO] Shutting down: Master
[2016-02-08 14:58:22 +0900] [3470] [INFO] Starting gunicorn 19.4.3
[2016-02-08 14:58:22 +0900] [3470] [INFO] Listening at: unix:/tmp/superlists-staging.mysite.com.socket (3470)
[2016-02-08 14:58:22 +0900] [3470] [INFO] Using worker: sync
[2016-02-08 14:58:22 +0900] [3474] [INFO] Booting worker with pid: 3474

I want see error loging, What can I do?

like image 539
Junsu Kim Avatar asked Feb 08 '16 06:02

Junsu Kim


People also ask

Where are Gunicorn error logs?

Gunicorn Logs This log file is located at /var/log/cloudify/rest/gunicorn-access.

How do you check Gunicorn errors?

Gunicorn error logs You can change how verbose these messages are using the "loglevel" setting, which can be set to log more info using the "debug" level, or only errors, using the "error" level, etc.

How do you check logs on Gunicorn?

To watch the logs in the console you need to use the option --log-file=- . In version 19.2, Gunicorn logs to the console by default again.

How does Gunicorn work with Django?

Gunicorn takes care of everything which happens in-between the web server and your web application. This way, when coding up your a Django application you don't need to find your own solutions for: communicating with multiple web servers. reacting to lots of web requests at once and distributing the load.


2 Answers

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

like image 80
Sumit Badsara Avatar answered Sep 22 '22 17:09

Sumit Badsara


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

like image 36
tutuDajuju Avatar answered Sep 21 '22 17:09

tutuDajuju