当我告诉它时,如何使用 Django 记录器来记录回溯?

发布于 2024-10-14 16:04:04 字数 163 浏览 7 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

春风十里 2024-10-21 16:04:04

您可以使用 python 的日志记录机制:

import logging
...

logger = logging.getLogger("blabla")
...

try:
    print blah # You can use logger.debug("blah") instead of print
except KeyError:
    logger.exception("An error occurred")

这将打印堆栈跟踪并与 apache 一起使用。

You can use python's logging mechanism:

import logging
...

logger = logging.getLogger("blabla")
...

try:
    print blah # You can use logger.debug("blah") instead of print
except KeyError:
    logger.exception("An error occurred")

This will print the stack trace and will work with apache.

我喜欢麦丽素 2024-10-21 16:04:04

如果您运行的是 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, call logger = logging.getLogger(__name__) and then call logger.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 to sys.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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文