python 将所有错误记录到 Django 站点上的控制台或文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/690723/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Log all errors to console or file on Django site
提问by jlpp
How can I get Django 1.0 to write allerrors to the console or a log file when running runserver in debug mode?
在调试模式下运行 runserver 时,如何让 Django 1.0 将所有错误写入控制台或日志文件?
I've tried using a middleware class with process_exception function as described in the accepted answer to this question:
我已经尝试使用带有 process_exception 函数的中间件类,如该问题的已接受答案中所述:
How do you log server errors on django sites
The process_exception function is called for some exceptions (eg: assert(False) in views.py) but process_exception is not getting called for other errors like ImportErrors (eg: import thisclassdoesnotexist in urs.py). I'm new to Django/Python. Is this because of some distinction between run-time and compile-time errors? But then I would expect runserver to complain if it was a compile-time error and it doesn't.
process_exception 函数会为某些异常调用(例如:views.py 中的 assert(False)),但 process_exception 不会因其他错误(例如 ImportErrors)而被调用(例如:import thisclassdoesnotexist in urs.py)。我是 Django/Python 的新手。这是因为运行时和编译时错误之间的某些区别吗?但是如果它是一个编译时错误,我希望 runserver 会抱怨,而事实并非如此。
I've watched Simon Willison's fantastic presentation on Django debugging (http://simonwillison.net/2008/May/22/debugging/) but I didn't see an option that would work well for me.
我看过 Simon Willison 关于 Django 调试的精彩演讲 ( http://simonwillison.net/2008/May/22/debugging/),但我没有看到适合我的选项。
In case it's relevant, I'm writing a Facebook app and Facebook masks HTTP 500 errors with their own message rather than showing Django's awesomely informative 500 page. So I need a way for alltypes of errors to be written to the console or file.
以防万一,我正在编写一个 Facebook 应用程序,而 Facebook 用自己的消息掩盖了 HTTP 500 错误,而不是显示 Django 信息量非常大的 500 页面。所以我需要一种将所有类型的错误写入控制台或文件的方法。
Edit:I guess my expectation is that if Django can return a 500 error page with lots of detail when I have a bad import (ImportError) in urls.py, it should be able to write the same detail to the console or a file without having to add any additional exception handling to the code. I've never seen exception handling around import statements.
编辑:我想我的期望是,当我在 urls.py 中导入错误(ImportError)时,如果 Django 可以返回一个包含大量细节的 500 错误页面,它应该能够将相同的细节写入控制台或没有必须向代码添加任何额外的异常处理。我从未见过围绕 import 语句的异常处理。
Thanks, Jeff
谢谢,杰夫
采纳答案by Jason Baker
It's a bit extreme, but for debugging purposes, you can turn on the DEBUG_PROPAGATE_EXCEPTIONS
setting. This will allow you to set up your own error handling. The easiest way to set up said error handling would be to override sys.excepthook. This will terminate your application, but it will work. There may be things you can do to make this not kill your app, but this will depend on what platform you're deploying this for. At any rate, never use this in production!
有点极端,但是为了调试,可以开启DEBUG_PROPAGATE_EXCEPTIONS
设置。这将允许您设置自己的错误处理。设置所述错误处理的最简单方法是覆盖sys.excepthook。这将终止您的应用程序,但它会起作用。您可能可以采取一些措施使这不会杀死您的应用程序,但这取决于您要为其部署的平台。无论如何,永远不要在生产中使用它!
For production, you're pretty much going to have to have extensive error handling in place. One technique I've used is something like this:
对于生产,您几乎必须进行广泛的错误处理。我使用过的一种技术是这样的:
>>> def log_error(func):
... def _call_func(*args, **argd):
... try:
... func(*args, **argd)
... except:
... print "error" #substitute your own error handling
... return _call_func
...
>>> @log_error
... def foo(a):
... raise AttributeError
...
>>> foo(1)
error
If you use log_error as a decorator on your view, it will automatically handle whatever errors happened within it.
如果您在视图上使用 log_error 作为装饰器,它将自动处理其中发生的任何错误。
The process
_
exception function is called for some exceptions (eg: assert(False) in views.py) but process_
exception is not getting called for other errors like ImportErrors (eg: import thisclassdoesnotexist in urs.py). I'm new to Django/Python. Is this because of some distinction between run-time and compile-time errors?
_
对于某些异常(例如:views.py 中的 assert(False))会调用进程异常函数,但_
不会因其他错误(例如 ImportErrors )调用进程异常函数(例如:在urs.py 中不存在 import thisclass)。我是 Django/Python 的新手。这是因为运行时和编译时错误之间的某些区别吗?
In Python, all errors are run-time errors. The reason why this is causing problems is because these errors occur immediately when the module is imported before your view is ever called. The first method I posted will catch errors like these for debugging. You might be able to figure something out for production, but I'd argue that you have worse problems if you're getting ImportErrors in a production app (and you're not doing any dynamic importing).
在 Python 中,所有错误都是运行时错误。这导致问题的原因是因为在调用视图之前导入模块时会立即发生这些错误。我发布的第一种方法将捕获此类错误以进行调试。您可能能够为生产找出一些方法,但我认为如果您在生产应用程序中遇到 ImportErrors(并且您没有进行任何动态导入),那么您会遇到更严重的问题。
A tool like pylintcan help you eliminate these kinds of problems though.
不过,pylint 之类的工具可以帮助您消除此类问题。
回答by Carl Meyer
The process_exception function is called for some exceptions (eg: assert(False) in views.py) but process_exception is not getting called for other errors like ImportErrors (eg: import thisclassdoesnotexist in urs.py). I'm new to Django/Python. Is this because of some distinction between run-time and compile-time errors?
process_exception 函数会为某些异常调用(例如:views.py 中的 assert(False)),但 process_exception 不会因其他错误(例如 ImportErrors)而被调用(例如:import thisclassdoesnotexist in urs.py)。我是 Django/Python 的新手。这是因为运行时和编译时错误之间的某些区别吗?
No, it's just because process_exception middleware is only called if an exception is raised in the view.
不,这只是因为process_exception 中间件仅在 view 中引发异常时才被调用。
I think DEBUG_PROPAGATE_EXCEPTIONS(as mentioned first by Jason Baker) is what you need here, but I don't think you don't need to do anything additional (i.e. sys.excepthook, etc) if you just want the traceback dumped to console.
我认为DEBUG_PROPAGATE_EXCEPTIONS(如 Jason Baker 首先提到的)是你在这里需要的,但我认为你不需要做任何额外的事情(即 sys.excepthook 等),如果你只是想将回溯转储到控制台。
If you want to do anything more complex with the error (i.e. dump it to file or DB), the simplest approach would be the got_request_exception signal, which Django sends for any request-related exception, whether it was raised in the view or not.
如果你想对错误做任何更复杂的事情(即把它转储到文件或数据库),最简单的方法是got_request_exception 信号,Django 发送任何与请求相关的异常,无论它是否在视图中引发。
The get_responseand handle_uncaught_exceptionmethods of django.core.handlers.BaseHandler are instructive (and brief) reading in this area.
django.core.handlers.BaseHandler的get_response和handle_uncaught_exception方法在这方面具有指导性(和简要)阅读。
without having to add any additional exception handling to the code. I've never seen exception handling around import statements.
无需向代码添加任何额外的异常处理。我从未见过围绕 import 语句的异常处理。
Look around a bit more, you'll see it done (often in cases where you want to handle the absence of a dependency in some particular way). That said, it would of course be quite ugly if you had to go sprinkling additional try-except blocks all over your code to make a global change to how exceptions are handled!
再多看看,你会看到它完成了(通常在你想以某种特定方式处理依赖项缺失的情况下)。也就是说,如果您不得不在代码中添加额外的 try-except 块以对异常的处理方式进行全局更改,那当然会非常难看!
回答by S.Lott
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.
首先,您可以通过异常日志看到很少的编译时错误。如果您的 Python 代码没有有效的语法,它会在打开日志进行写入之前很久就死了。
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.
在 Django runserver 模式下,“print”语句写入标准输出,您可以看到。然而,这不是一个好的长期解决方案,所以不要指望它。
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.
但是,当 Django 在 Apache 下运行时,这取决于您使用的是哪个插件。mod_python 不容易处理。可以强制 mod_wsgi 将 stdout 和 stderr 发送到日志文件。
Your best bet, however, is the loggingmodule. Put an initialization into your top-level urls.py
to configure logging. (Or, perhaps, your settings.py
)
但是,您最好的选择是日志记录模块。将初始化放入顶层urls.py
以配置日志记录。(或者,也许,你的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.
确保您发出的每个 Web 服务调用都有一个 try/except 块,并将异常写入日志。
回答by jaimechen
回答by jaimechen
If you are on a *nix system you could
如果你在 *nix 系统上,你可以
write to a log (eg. mylog.txt) in python then run "tail -f mylog.txt" in the console
在python中写入日志(例如mylog.txt)然后在控制台中运行“tail -f mylog.txt”
this is a handy way to view any kind of log in near real time
这是一种近乎实时地查看任何类型的登录信息的便捷方式