pythondjangologgingpython-logging

Python/Django: log to console under runserver, log to file under Apache


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.


Solution

  • Text printed to stderr will show up in httpd's error log when running under mod_wsgi. You can either use print directly, or use logging instead.

    python 3:

    print("Goodbye cruel world!", file=sys.stderr)
    

    python 2:

    print >>sys.stderr, 'Goodbye, cruel world!'