In this port we will be exploring possible logging options when having deployed our Django Channels app to AWS Elastic Beanstalk (see previous posts). An excellent guide to Logging with Django can as always be found in their docs. Here we will explore a simple logging setup that has a catch-all for both Django and Channels loggers. A quick reminder of the setup we have on our AWS Elastic Beanstalk server. HTTP requests are being server by Django as WSGI through Apache wsgi module. At the same time we are serving WebSocket connections using Daphne on a local port 5000 and several workers using Supervisord. Daphne and the workers are communicating using a Redis transport layer. Following Django own examples and resources pulled from around the web we add the following minimalistic Logging setup to our settings.py.
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'handlers': {
'console': {
'level': 'INFO',
'class': 'logging.StreamHandler',
},
},
'loggers': {
'django': {
'handlers': ['console'],
'level': 'INFO',
'propagate': True,
},
},
}
This handler will use the standard stream to log all messages logged to the django logger. This logger is the standard Django logger, and all Django's own messages come through this channel. To use the logger we can do the following:
import logging
logger = logging.getLogger(**'django'**)
logger.info('This message **will** show up in the log')
logger.debug('This message **will not** show up in the log')
try:
open('non-existing-file')# a function that we know will fail
except:
logger.exception('This will log the entire **stack trace**')
Notice we are using the 'django' logger. Every message with a rating of INFO or higher will be logged. We can log the entire stack trace using logger.exception.
The beauty of using the standard stream to log is that it will log to whatever the current output stream is. For everything HTTP Django related it will output to the standard Apache logs /var/log/httpd/access_log and /var/log/httpd/error_log. For everything Channels-related it will output to whatever we configured in the supervisord.conf-file, in our case. /tmp/workers.out.log and /tmp/daphne.out.log. Now no messages from either Django, Channels or from our app will get lost.