How can I send trace messages to the console (like print
) when I'm running my Django app under manage.py runserver
, but have those messages sent to a log file when I'm running the app under Apache?
I reviewed Django logging and although I was impressed with its flexibility and configurability for advanced uses, I'm still stumped with how to handle my simple use-case.
You can configure logging in your
settings.py
file.One example:
However that's dependent upon setting DEBUG, and maybe you don't want to have to worry about how it's set up. See this answer on How can I tell whether my Django application is running on development server or not? for a better way of writing that conditional. Edit: the example above is from a Django 1.1 project, logging configuration in Django has changed somewhat since that version.
Here's a Django logging-based solution. It uses the DEBUG setting rather than actually checking whether or not you're running the development server, but if you find a better way to check for that it should be easy to adapt.
see https://docs.djangoproject.com/en/dev/topics/logging/ for details.
Text printed to stderr will show up in httpd's error log when running under mod_wsgi. You can either use
print
directly, or uselogging
instead.I use this:
logging.conf:
testapp.py:
This works quite well in my local.py, saves me messing up the regular logging:
You can do this pretty easily with
tagalog
(https://github.com/dorkitude/tagalog)For instance, while the standard python module writes to a file object opened in append mode, the App Engine module (https://github.com/dorkitude/tagalog/blob/master/tagalog_appengine.py) overrides this behavior and instead uses
logging.INFO
.To get this behavior in an App Engine project, one could simply do:
You could extend the module yourself and overwrite the log function without much difficulty.