I just deployed my Django (1.6) project with gunicorn and Nginx.
It seems to be working fine but I have one page were I\'m getting an HTTP 500 error and I can\'t find an
With following logging configuration, your errors will start showing up in Gunicorn output(undaemonized) or runserver even when DEBUG is False. They anyways should be showing up when DEBUG is True.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse',
},
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue',
},
},
'formatters': {
'django.server': {
'()': 'django.utils.log.ServerFormatter',
'format': '[%(server_time)s] %(message)s',
}
},
'handlers': {
'console': {
'level': 'INFO',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
},
# Custom handler which we will use with logger 'django'.
# We want errors/warnings to be logged when DEBUG=False
'console_on_not_debug': {
'level': 'WARNING',
'filters': ['require_debug_false'],
'class': 'logging.StreamHandler',
},
'django.server': {
'level': 'INFO',
'class': 'logging.StreamHandler',
'formatter': 'django.server',
},
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django': {
'handlers': ['console', 'mail_admins', 'console_on_not_debug'],
'level': 'INFO',
},
'django.server': {
'handlers': ['django.server'],
'level': 'INFO',
'propagate': False,
},
}
}
If you want to see the Django errors in gunicorn error log, run gunicorn with --capture-output.
http://docs.gunicorn.org/en/stable/settings.html#capture-output
There are two confusions involved when logging:
runserver provide better log than gunicornsettings.DEBUG=True provide better log than settings.DEBUG=FalseAny log record you see with runserver can be seen with Gunicorn too as long as you have appropriate logging configuration.
Any log record you see with DEBUG=True can be seen while DEBUG=False too as long as you have appropriate logging configuration.
You can see default Django logging configuration at:
https://github.com/django/django/blob/1.10.8/django/utils/log.py#L18
It looks like: (I have stripped out parts which don't concern this answer)
DEFAULT_LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'require_debug_false': {
'()': 'django.utils.log.RequireDebugFalse',
},
'require_debug_true': {
'()': 'django.utils.log.RequireDebugTrue',
},
},
'handlers': {
'console': {
'level': 'INFO',
'filters': ['require_debug_true'],
'class': 'logging.StreamHandler',
},
'mail_admins': {
'level': 'ERROR',
'filters': ['require_debug_false'],
'class': 'django.utils.log.AdminEmailHandler'
}
},
'loggers': {
'django': {
'handlers': ['console', 'mail_admins'],
'level': 'INFO',
},
}
}
What this says is:
Send django logger log record to handlers console and mail_admins.
Handler console has a filter require_debug_true on it. When settings.DEBUG is True, then handler console sends/prints the log on the Stream (because of logging.StreamHandler).
When settings.DEBUG is False, then handler console ignores the log message sent to it by logger django.
If you want logs to be printed with DEBUG=False too, then add a handler and make logger django use it.
Handler would look like:
'console_on_not_debug': {
'level': 'WARNING',
'filters': ['require_debug_false'],
'class': 'logging.StreamHandler',
},
And use this handler with logger django:
'django': {
'handlers': ['console', 'mail_admins', 'console_on_not_debug'],
'level': 'INFO',
},
You can see the entire snippet in short answer.
With this, the logs will be printed on stream irrespective of if you are using runserver or gunicorn.
If you want the logs to be shown in gunicorn error log, then you need to run gunicorn with --capture-output.