Log all errors to console or file on Django site

前端 未结 5 1131
臣服心动
臣服心动 2020-12-09 17:50

How can I get Django 1.0 to write all errors to the console or a log file when running runserver in debug mode?

I\'ve tried using a middleware class with pr

5条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-09 18:42

    First, there are very few compile-time errors that you'll see through an exception log. If your Python code doesn't have valid syntax, it dies long before logs are opened for writing.

    In Django runserver mode, a "print" statement writes to stdout, which you can see. This is not a good long-term solution, however, so don't count on it.

    When Django is running under Apache, however, it depends on which plug-in you're using. mod_python isn't easy to deal with. mod_wsgi can be coerced into sending stdout and stderr to a log file.

    Your best bet, however, is the logging module. Put an initialization into your top-level urls.py to configure logging. (Or, perhaps, your settings.py)

    Be sure that every module has a logger available for writing log messages.

    Be sure that every web services call you make has a try/except block around it, and you write the exceptions to your log.

提交回复
热议问题