"Don't panic. Check the error logs."
"Logs? Django doesn't do logs."
"Python does. You have to add them into your project."
"Yes, but Django doesn't work. Now I am panicking."
And...cut. Beautifully acted all around. The point of the above is, there is a potential chicken-and-egg scenario brewing when you:
- Have a nonfunctioning Django app.
- Install logging to debug it.
- Can't run the application to test the logging...
- to run the application.
For brevity and to add a couple of caveats, I have combined what was published elsewhere into this quick how-to. Simply put, it will get you up and logging straight away. Just add it to your settings.py:
import loggingThere are some fine debugging options out there e.g. djangologging and the django debug toolbar. The advantage of the above is, it is simply a python module so you can't get more native than that.
import logging.handlers
logger = logging.getLogger('project_logger')
logger.setLevel(logging.INFO)
# IMPORTANT, make sure python can write to the below log-file, or you will get an error
LOG_FILENAME = '/root/to/your/logging/dir/and/log-file'
LOG_MSG_FORMAT = '%(asctime)s %(levelname)s %(message)s'
handler = logging.FileHandler(LOG_FILENAME)
formatter = logging.Formatter(LOG_MSG_FORMAT)
handler.setFormatter(formatter)
logger.addHandler(handler)
logger.info('testing logging')
Feel free to try whichever you find is best and to comment on this option.
No comments:
Post a Comment