当我告诉它时,如何使用 Django 记录器来记录回溯?
try:
print blah
except KeyError:
traceback.print_exc()
我以前都是这样调试的。我会打印到控制台。现在,我想记录所有内容而不是打印,因为 Apache 不允许打印。那么,我如何记录整个回溯呢?
try:
print blah
except KeyError:
traceback.print_exc()
I used to debug like this. I'd print to the console. Now, I want to log everything instead of print, since Apache doesn't allow printing. So, how do I log this entire traceback?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 python 的日志记录机制:
这将打印堆栈跟踪并与 apache 一起使用。
You can use python's logging mechanism:
This will print the stack trace and will work with apache.
如果您运行的是 Django 的 trunk 版本(或发布时的 1.3),则内置有许多默认日志记录配置,它们与 Python 的标准日志记录模块集成。为此,您需要做的就是
导入日志记录
,调用logger =logging.getLogger(__name__)
,然后调用logger.exception(msg)
> 您将收到消息和堆栈跟踪。 Django 日志记录功能 和 Python 的 logger.exception 方法 将是方便的参考。如果您不想使用 Python 的日志记录模块,也可以
import sys
并写入sys.stderr
而不是使用 print。在命令行上,它会显示在屏幕上,当在 Apache 下运行时,它会进入错误日志。If you're running Django's trunk version (or 1.3 when it's released), there are a number of default logging configurations built in which are integrated with Python's standard logging module. For that, all you need to do is
import logging
, calllogger = logging.getLogger(__name__)
and then calllogger.exception(msg)
and you'll get both your message and a stack trace. Docs for Django's logging functionality and Python's logger.exception method would be handy references.If you don't want to use Python's logging module, you can also
import sys
and write tosys.stderr
rather than using print. On the command line this goes to the screen, when running under Apache it'll go into your error logs.