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?
Gunicorn Logs This log file is located at /var/log/cloudify/rest/gunicorn-access.
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.
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.
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.
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)
sudo journalctl -u nginx
sudo less /var/log/nginx/access.log
sudo less /var/log/nginx/error.log
sudo journalctl -u gunicorn
sudo journalctl -u gunicorn.socket
Reference
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With