how do I get django runserver to show me DeprecationWarnings and other useful messages?
I've recently updated my django installation from 1.2 to 1.3. On my developer system I didn't get any warnings about deprecated calls. But once I moved the code onto my production apache server, I saw many 'Deprecat开发者_如何学运维ionWarning' messages in my apache logs. So how do I have to call runserver to these these messages too?
Currently I call it like this:
python manage.py runserver --verbosity 2
Python 2.7 disables the display of DeprecationWarning by default
To re-enable it, set environment variable PYTHONWARNINGS to "d":
export PYTHONWARNINGS="d"; ./manage.py runserver
To have your development server and your tests fail for DeprecationWarnings in order to locate and fix them, you can convert them to errors by adding
if settings.DEBUG:
import warnings
warnings.simplefilter('error', DeprecationWarning)
# these are less urgent but could also be enabled
# warnings.simplefilter('error', PendingDeprecationWarning)
to your top-level urls.py.
I prefer this approach because it fails in tests and lets me locate the deprecated code one by one in an automated fashion.
The runserver command ignores the verbosity option: https://code.djangoproject.com/ticket/15132
I'd recommend setting up a logger and directing the output to stderr: https://docs.djangoproject.com/en/1.3/topics/logging/
For example:
import logging
logger = logging.getLogger('django') # Django's catch-all logger
hdlr = logging.StreamHandler() # Logs to stderr by default
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
hdlr.setFormatter(formatter)
logger.addHandler(hdlr)
logger.setLevel(logging.WARNING)
精彩评论